********************************************************************************
********************************************************************************
**** Conflict on the campaign trail? How campaign effort and electoral
**** competitiveness shape affective polarization
**** 
**** Replication files 
**** Data reshaping, preparation and descriptives
****
********************************************************************************
********************************************************************************

clear 

* set working directory 

cd ""

**# ----- CONSTITUENCY DATA CODING --------

**# 1. Coding CI index for 2019

* load in the constituency-level election result data:

use BES-2019-General-Election-results-file-v1.1.dta, clear

* begin by dropping the speaker's constituency (no contest from the major parties here)

numlabel, add

tab Winner19
drop if Winner19==9

*** next, we calculate the number of effective parties per constituency. 

* recode the vote share variables into percentages: 

gen ConVs19=Con19/100
gen LabVs19=Lab19/100
gen LDVs19=LD19/100
gen UKIPVs19=UKIP19/100
gen GrnVs19=Green19/100
gen SNPVs19=SNP19/100
gen PCVs19=PC19/100
gen OtherVs19=Other19/100
gen BrexitVs19=Brexit19/100

* recode missing as 0s

recode ConVs19 LabVs19 PCVs19 SNPVs19 OtherVs19 GrnVs19 UKIPVs19 LDVs19 BrexitVs19 (.=0)

* calculate number of effective parties per constituency


bysort ConstituencyName: gen eff_part_19 = 1/((ConVs19)^2 + (LabVs19)^2 + (LDVs19)^2 + (UKIPVs19)^2 + (GrnVs19)^2 + (SNPVs19)^2 + (PCVs19)^2 + (OtherVs19)^2)

tab eff_part_19

* now round the effective party scores so that the variable has only integer values:

gen eff_parties19=.
replace eff_parties19 = 1 if eff_part_19<1.5
replace eff_parties19 = 2 if eff_part_19>=1.5 & eff_part_19<2.5 
replace eff_parties19 = 3 if ((eff_part_19>=2.5) & (eff_part_19<3.5))
replace eff_parties19 = 4 if ((eff_part_19>3.5) & (eff_part_19<4.5))
replace eff_parties19 = 5 if eff_part_19>4.5
tab eff_parties19

** the next step is to calculate the effective party vote shares in each constituency. 

* begin by coding empty variables for each: 

gen eff_party_19_1=0
gen eff_party_19_2=0 if (eff_parties19==2)|(eff_parties19==3)|(eff_parties19==4)|(eff_parties==5)
gen eff_party_19_3=0 if (eff_parties19==3)|(eff_parties19==4)
gen eff_party_19_4=0 if (eff_parties19==4)|(eff_parties==5)
gen eff_party_19_5=0 if eff_parties==5

* The next step is to determine which party finished second/third/fourth/fifth in each constituency. 
* For this second party indicator, the parties are coded as follows. 1 - Con; 2 - Lab; 3 - LD; 4 - SNP; 5 - PC; 6 - UKIP; 7 - Grn; 8 - Other; 9 - Brexit.

* coding an empty second party indicator

gen Second19=.

* For those constituencies in which Labour was winner: 


replace Second19=1 if ((Winner19==2) & (ConVs19>LDVs19 & ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>SNPVs19 & ConVs19>PCVs19 & ConVs19>OtherVs19))
replace Second19=3 if ((Winner19==2) & (LDVs19>ConVs19 & LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>SNPVs19 & LDVs19>PCVs19 & LDVs19>OtherVs19))
replace Second19=4 if ((Winner19==2) & (SNPVs19>ConVs19 & SNPVs19>LDVs19 & SNPVs19>UKIPVs19 & SNPVs19>BrexitVs19 & SNPVs19>GrnVs19 & SNPVs19>OtherVs19))
replace Second19=5 if ((Winner19==2) & (PCVs19>ConVs19 & PCVs19>LDVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Second19=6 if ((Winner19==2) & (UKIPVs19>ConVs19 & UKIPVs19>LDVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>SNPVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Second19=7 if ((Winner19==2) & (GrnVs19>ConVs19 & GrnVs19>LDVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>SNPVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Second19=8 if ((Winner19==2) & (OtherVs19>ConVs19 & OtherVs19>LDVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19))
replace Second19=9 if ((Winner19==2) & (BrexitVs19>ConVs19 & BrexitVs19>LDVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>SNPVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19))

* For those constituencies in which the Conservatives were winners:

replace Second19=2 if ((Winner19==1) & (LabVs19>LDVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>SNPVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19))
replace Second19=3 if ((Winner19==1) & (LDVs19>LabVs19 & LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>SNPVs19 & LDVs19>PCVs19 & LDVs19>OtherVs19))
replace Second19=4 if ((Winner19==1) & (SNPVs19>LabVs19 & SNPVs19>LDVs19 & SNPVs19>UKIPVs19 & SNPVs19>BrexitVs19 & SNPVs19>GrnVs19 & SNPVs19>OtherVs19))
replace Second19=5 if ((Winner19==1) & (PCVs19>LabVs19 & PCVs19>LDVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Second19=6 if ((Winner19==1) & (UKIPVs19>LabVs19 & UKIPVs19>LDVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>SNPVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Second19=7 if ((Winner19==1) & (GrnVs19>LabVs19 & GrnVs19>LDVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>SNPVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Second19=8 if ((Winner19==1) & (OtherVs19>LabVs19 & OtherVs19>LDVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19))
replace Second19=9 if ((Winner19==1) & (BrexitVs19>LabVs19 & BrexitVs19>LDVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>SNPVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19))

* LD as winner: 
replace Second19=2 if ((Winner19==3) & (LabVs19>ConVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>SNPVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19))
replace Second19=1 if ((Winner19==3) & (ConVs19>LabVs19 & ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>SNPVs19 & ConVs19>PCVs19 & ConVs19>OtherVs19))
replace Second19=4 if ((Winner19==3) & (UKIPVs19>LabVs19 & UKIPVs19>ConVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>SNPVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Second19=5 if ((Winner19==3) & (BrexitVs19>LabVs19 & BrexitVs19>ConVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>SNPVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19))
replace Second19=6 if ((Winner19==3) & (GrnVs19>LabVs19 & GrnVs19>ConVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>SNPVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Second19=7 if ((Winner19==3) & (PCVs19>LabVs19 & PCVs19>ConVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Second19=8 if ((Winner19==3) & (OtherVs19>LabVs19 & OtherVs19>ConVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19))

*LD as winner:
replace Second19=1 if ((Winner19==3) & (ConVs19>LabVs19 & ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>SNPVs19 & ConVs19>PCVs19 & ConVs19>OtherVs19))
replace Second19=2 if ((Winner19==3) & (LabVs19>ConVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>SNPVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19))
replace Second19=4 if ((Winner19==3) & (SNPVs19>LabVs19 & SNPVs19>ConVs19 & SNPVs19>UKIPVs19 & SNPVs19>BrexitVs19 & SNPVs19>GrnVs19 & SNPVs19>OtherVs19))
replace Second19=5 if ((Winner19==3) & (PCVs19>LabVs19 & PCVs19>ConVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Second19=6 if ((Winner19==3) & (UKIPVs19>LabVs19 & UKIPVs19>ConVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>SNPVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Second19=7 if ((Winner19==3) & (GrnVs19>LabVs19 & GrnVs19>ConVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>SNPVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Second19=8 if ((Winner19==3) & (OtherVs19>LabVs19 & OtherVs19>ConVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19))
replace Second19=9 if ((Winner19==3) & (BrexitVs19>LabVs19 & BrexitVs19>ConVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>SNPVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19))

*SNP as winner:
replace Second19=1 if ((Winner19==4) & (ConVs19>LabVs19 & ConVs19>UKIPVs19 & ConVs19>GrnVs19 & ConVs19>OtherVs19 & ConVs19>LDVs19 & ConVs19>BrexitVs19))
replace Second19=2 if ((Winner19==4) & (LabVs19>ConVs19 & LabVs19>UKIPVs19 & LabVs19>GrnVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19 & LabVs19>LDVs19 & LabVs19>BrexitVs19))
replace Second19=3 if ((Winner19==4) & (LDVs19>LabVs19 & LDVs19>UKIPVs19 & LDVs19>GrnVs19 & LDVs19>OtherVs19 & LDVs19>ConVs19 & LDVs19>BrexitVs19))
replace Second19=6 if ((Winner19==4) & (UKIPVs19>LabVs19 & UKIPVs19>ConVs19 & UKIPVs19>GrnVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19 & UKIPVs19>BrexitVs19))
replace Second19=7 if ((Winner19==4) & (GrnVs19>LabVs19 & GrnVs19>ConVs19 & GrnVs19>UKIPVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19 & GrnVs19>BrexitVs19))
replace Second19=8 if ((Winner19==4) & (OtherVs19>LabVs19 & OtherVs19>ConVs19 & OtherVs19>UKIPVs19 & OtherVs19>PCVs19 & OtherVs19>GrnVs19 & OtherVs19>BrexitVs19))

*PC as winner:
replace Second19=1 if ((Winner19==5) & (ConVs19>LabVs19 & ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>SNPVs19 & ConVs19>LDVs19 & ConVs19>OtherVs19))
replace Second19=2 if ((Winner19==5) & (LabVs19>ConVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>SNPVs19 & LabVs19>LDVs19 & LabVs19>OtherVs19))
replace Second19=3 if ((Winner19==5) & (LDVs19>LabVs19 & LDVs19>ConVs19 & LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>OtherVs19))
replace Second19=6 if ((Winner19==5) & (UKIPVs19>LabVs19 & UKIPVs19>ConVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>SNPVs19 & UKIPVs19>LDVs19 & UKIPVs19>OtherVs19))
replace Second19=7 if ((Winner19==5) & (GrnVs19>LabVs19 & GrnVs19>ConVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>SNPVs19 & GrnVs19>LDVs19 & GrnVs19>OtherVs19))
replace Second19=8 if ((Winner19==5) & (OtherVs19>LabVs19 & OtherVs19>ConVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>LDVs19))
replace Second19=9 if ((Winner19==5) & (BrexitVs19>LabVs19 & BrexitVs19>ConVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>SNPVs19 & BrexitVs19>LDVs19 & BrexitVs19>OtherVs19))

*Green as winner: 
tab pano if Winner19==7
tab LabVs19 if Winner19==7
tab ConVs19 if Winner19==7
tab BrexitVs19 if Winner19==7
tab UKIPVs19 if Winner19==7

replace Second19=2 if Winner19==7

tab Winner19
tab Second19

label define con_label 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other" 9 "Brexit"
label values Second19 con_label
tab Second19

* Coding an empty third party indicator 


gen Third19=.

*Conservatives as winners:
replace Third19=3 if ((Winner19==1) & (Second19==2) & (LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>PCVs19 & LDVs19>OtherVs19))
replace Third19=4 if ((Winner19==1) & (Second19==2) & (SNPVs19>LDVs19 & SNPVs19>UKIPVs19 & SNPVs19>BrexitVs19 & SNPVs19>GrnVs19 & SNPVs19>OtherVs19))
replace Third19=5 if ((Winner19==1) & (Second19==2) & (PCVs19>LDVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Third19=6 if ((Winner19==1) & (Second19==2) & (UKIPVs19>LDVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Third19=7 if ((Winner19==1) & (Second19==2) & (GrnVs19>LDVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Third19=8 if ((Winner19==1) & (Second19==2) & (OtherVs19>LDVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19 & OtherVs19>SNPVs19))
replace Third19=9 if ((Winner19==1) & (Second19==2) & (BrexitVs19>LDVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19 & BrexitVs19>SNPVs19))

replace Third19=2 if ((Winner19==1) & (Second19==3) & (LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19 & LabVs19>SNPVs19))
replace Third19=4 if ((Winner19==1) & (Second19==3) & (SNPVs19>LabVs19 & SNPVs19>UKIPVs19 & SNPVs19>BrexitVs19 & SNPVs19>GrnVs19 & SNPVs19>OtherVs19))
replace Third19=5 if ((Winner19==1) & (Second19==3) & (PCVs19>LabVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Third19=6 if ((Winner19==1) & (Second19==3) & (UKIPVs19>LabVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19 & UKIPVs19>SNPVs19))
replace Third19=7 if ((Winner19==1) & (Second19==3) & (GrnVs19>LabVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19 & GrnVs19>SNPVs19))
replace Third19=8 if ((Winner19==1) & (Second19==3) & (OtherVs19>LabVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19 & OtherVs19>SNPVs19))
replace Third19=9 if ((Winner19==1) & (Second19==3) & (BrexitVs19>LabVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19 & BrexitVs19>SNPVs19))

replace Third19=2 if ((Winner19==1) & (Second19==4) & (LabVs19>LDVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>OtherVs19))
replace Third19=3 if ((Winner19==1) & (Second19==4) & (LDVs19>LabVs19 & LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>OtherVs19))

replace Third19=2 if ((Winner19==1) & (Second19==8) & (LabVs19>LDVs19 & LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>SNPVs19 & LabVs19>PCVs19))

*Lab as winners: 
tab Country if Winner19==2 & Second19==3

replace Third19=3 if ((Winner19==2) & (Second19==1) & (LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>PCVs19 & LDVs19>OtherVs19))
replace Third19=5 if ((Winner19==2) & (Second19==1) & (PCVs19>LDVs19 & PCVs19>UKIPVs19 & PCVs19>BrexitVs19 & PCVs19>GrnVs19 & PCVs19>OtherVs19))
replace Third19=6 if ((Winner19==2) & (Second19==1) & (UKIPVs19>LDVs19 & UKIPVs19>BrexitVs19 & UKIPVs19>GrnVs19 & UKIPVs19>PCVs19 & UKIPVs19>OtherVs19))
replace Third19=7 if ((Winner19==2) & (Second19==1) & (GrnVs19>LDVs19 & GrnVs19>UKIPVs19 & GrnVs19>BrexitVs19 & GrnVs19>PCVs19 & GrnVs19>OtherVs19))
replace Third19=8 if ((Winner19==2) & (Second19==1) & (OtherVs19>LDVs19 & OtherVs19>UKIPVs19 & OtherVs19>BrexitVs19 & OtherVs19>GrnVs19 & OtherVs19>PCVs19))
replace Third19=9 if ((Winner19==2) & (Second19==1) & (BrexitVs19>LDVs19 & BrexitVs19>UKIPVs19 & BrexitVs19>GrnVs19 & BrexitVs19>PCVs19 & BrexitVs19>OtherVs19))

replace Third19=1 if ((Winner19==2) & (Second19==3) & (ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>PCVs19 & ConVs19>OtherVs19))

tab pano if Winner19==2 & Second19==4
tab LDVs19 if pano==230
tab ConVs19 if pano==230
tab BrexitVs19 if pano==230

replace Third19=1 if pano==230

tab pano if Winner19==2 & Second19==7
tab PCVs19 if pano==102
tab SNPVs19 if pano==102
tab LDVs19 if pano==102
tab ConVs19 if pano==102
tab BrexitVs19 if pano==102
tab OtherVs19 if pano==102

replace Third19=1 if pano==102

tab PCVs19 if pano==206
tab SNPVs19 if pano==206
tab LDVs19 if pano==206
tab ConVs19 if pano==206
tab BrexitVs19 if pano==206
tab UKIPVs19 if pano==206
tab OtherVs19 if pano==206

replace Third19=1 if pano==206

tab pano if Winner19==2 & Second19==8

tab PCVs19 if pano==56
tab SNPVs19 if pano==56
tab LDVs19 if pano==56
tab ConVs19 if pano==56
tab BrexitVs19 if pano==56
tab UKIPVs19 if pano==56

replace Third19=1 if pano==56

tab pano if Winner19==2 & Second19==9

tab PCVs19 if pano==29
tab SNPVs19 if pano==29
tab LDVs19 if pano==29
tab ConVs19 if pano==29
tab BrexitVs19 if pano==29
tab UKIPVs19 if pano==29

replace Third19=1 if pano==29

tab PCVs19 if pano==30
tab SNPVs19 if pano==30
tab LDVs19 if pano==30
tab ConVs19 if pano==30
tab BrexitVs19 if pano==30
tab UKIPVs19 if pano==30

replace Third19=1 if pano==30

tab PCVs19 if pano==71
tab SNPVs19 if pano==71
tab LDVs19 if pano==71
tab ConVs19 if pano==71
tab BrexitVs19 if pano==71
tab UKIPVs19 if pano==71

replace Third19=1 if pano==71

*Lib Dem as winners:

replace Third19=2 if ((Winner19==3) & (Second19==1) & (LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19 & LabVs19>SNPVs19))
replace Third19=2 if ((Winner19==3) & (Second19==4) & (LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>PCVs19 & LabVs19>OtherVs19 & LabVs19>ConVs19))
replace Third19=1 if ((Winner19==3) & (Second19==4) & (ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>PCVs19 & ConVs19>OtherVs19 & ConVs19>LabVs19))

*SNP as winners:

replace Third19=2 if ((Winner19==4) & (Second19==1) & (LabVs19>UKIPVs19 & LabVs19>BrexitVs19 & LabVs19>GrnVs19 & LabVs19>OtherVs19 & LabVs19>LDVs19))
replace Third19=3 if ((Winner19==4) & (Second19==1) & (LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>OtherVs19 & LDVs19>LabVs19))

replace Third19=3 if ((Winner19==4) & (Second19==2) & (LDVs19>UKIPVs19 & LDVs19>BrexitVs19 & LDVs19>GrnVs19 & LDVs19>OtherVs19 & LDVs19>ConVs19))
replace Third19=1 if ((Winner19==4) & (Second19==2) & (ConVs19>UKIPVs19 & ConVs19>BrexitVs19 & ConVs19>GrnVs19 & ConVs19>OtherVs19 & ConVs19>LDVs19))

tab pano if Winner19==4 & Second19==3

tab PCVs19 if pano==209
tab LabVs19 if pano==209
tab LDVs19 if pano==209
tab ConVs19 if pano==209
tab BrexitVs19 if pano==209
tab UKIPVs19 if pano==209

replace Third19=1 if pano==209

tab PCVs19 if pano==479
tab LabVs19 if pano==479
tab LDVs19 if pano==479
tab ConVs19 if pano==479
tab BrexitVs19 if pano==479
tab UKIPVs19 if pano==479

replace Third19=1 if pano==479

* PC as winner.  

tab pano if Winner19==5 & Second19==1

tab PCVs19 if pano==131
tab LabVs19 if pano==131
tab LDVs19 if pano==131
tab ConVs19 if pano==131
tab BrexitVs19 if pano==131
tab UKIPVs19 if pano==131

replace Third19=2 if pano==131

tab PCVs19 if pano==135
tab LabVs19 if pano==135
tab LDVs19 if pano==135
tab ConVs19 if pano==135
tab BrexitVs19 if pano==135
tab UKIPVs19 if pano==135

replace Third19=3 if pano==135

tab PCVs19 if pano==217
tab LabVs19 if pano==217
tab LDVs19 if pano==217
tab ConVs19 if pano==217
tab BrexitVs19 if pano==217
tab UKIPVs19 if pano==217

replace Third19=2 if pano==217

tab pano if Winner19==5 & Second19==2

tab PCVs19 if pano==16
tab LabVs19 if pano==16
tab LDVs19 if pano==16
tab ConVs19 if pano==16
tab BrexitVs19 if pano==16
tab UKIPVs19 if pano==16

replace Third19=1 if pano==16

* Greens as winner: 

tab pano if Winner19==7 & Second19==2

tab PCVs19 if pano==98
tab LabVs19 if pano==98
tab LDVs19 if pano==98
tab ConVs19 if pano==98
tab BrexitVs19 if pano==98
tab UKIPVs19 if pano==98

replace Third19=1 if pano==98

tab Winner19 Second19
tab Third19

label values Third19 con_label
tab Third19

save CI_2019_v1.dta, replace

*** Fourth party indicator:

* coding an empty indicator 

gen Fourth19=.

tab pano if eff_parties19==4

* only 15 constituencies with 4 EPs, so these are coded manually:

tab ConVs19 if pano==71
tab LabVs19 if pano==71
tab SNPVs19 if pano==71
tab LDVs19 if pano==71
tab PCVs19 if pano==71
tab GrnVs19 if pano==71
tab BrexitVs19 if pano==71
tab UKIPVs19 if pano==71

replace Fourth19=5 if pano==71

tab ConVs19 if pano==135
tab LabVs19 if pano==135
tab SNPVs19 if pano==135
tab LDVs19 if pano==135
tab PCVs19 if pano==135
tab GrnVs19 if pano==135
tab BrexitVs19 if pano==135
tab UKIPVs19 if pano==135

replace Fourth19=2 if pano==135

tab ConVs19 if pano==195
tab LabVs19 if pano==195
tab SNPVs19 if pano==195
tab LDVs19 if pano==195
tab PCVs19 if pano==195
tab GrnVs19 if pano==195
tab BrexitVs19 if pano==195
tab UKIPVs19 if pano==195
tab OtherVs19 if pano==195

replace Fourth19=3 if pano==195

tab ConVs19 if pano==196
tab LabVs19 if pano==196
tab SNPVs19 if pano==196
tab LDVs19 if pano==196
tab PCVs19 if pano==196
tab GrnVs19 if pano==196
tab BrexitVs19 if pano==196
tab UKIPVs19 if pano==196
tab OtherVs19 if pano==196

replace Fourth19=8 if pano==196

tab ConVs19 if pano==221
tab LabVs19 if pano==221
tab SNPVs19 if pano==221
tab LDVs19 if pano==221
tab PCVs19 if pano==221
tab GrnVs19 if pano==221
tab BrexitVs19 if pano==221
tab UKIPVs19 if pano==221
tab OtherVs19 if pano==221

replace Fourth19=3 if pano==221

tab ConVs19 if pano==296
tab LabVs19 if pano==296
tab SNPVs19 if pano==296
tab LDVs19 if pano==296
tab PCVs19 if pano==296
tab GrnVs19 if pano==296
tab BrexitVs19 if pano==296
tab UKIPVs19 if pano==296
tab OtherVs19 if pano==296

replace Fourth19=3 if pano==296

tab ConVs19 if pano==303
tab LabVs19 if pano==303
tab SNPVs19 if pano==303
tab LDVs19 if pano==303
tab PCVs19 if pano==303
tab GrnVs19 if pano==303
tab BrexitVs19 if pano==303
tab UKIPVs19 if pano==303
tab OtherVs19 if pano==303

replace Fourth19=8 if pano==303

tab ConVs19 if pano==320
tab LabVs19 if pano==320
tab SNPVs19 if pano==320
tab LDVs19 if pano==320
tab PCVs19 if pano==320
tab GrnVs19 if pano==320
tab BrexitVs19 if pano==320
tab UKIPVs19 if pano==320
tab OtherVs19 if pano==320

replace Fourth19=3 if pano==320

tab ConVs19 if pano==323
tab LabVs19 if pano==323
tab SNPVs19 if pano==323
tab LDVs19 if pano==323
tab PCVs19 if pano==323
tab GrnVs19 if pano==323
tab BrexitVs19 if pano==323
tab UKIPVs19 if pano==323
tab OtherVs19 if pano==323

replace Fourth19=3 if pano==323

tab ConVs19 if pano==345
tab LabVs19 if pano==345
tab SNPVs19 if pano==345
tab LDVs19 if pano==345
tab PCVs19 if pano==345
tab GrnVs19 if pano==345
tab BrexitVs19 if pano==345
tab UKIPVs19 if pano==345
tab OtherVs19 if pano==345

replace Fourth19=3 if pano==345

tab ConVs19 if pano==408
tab LabVs19 if pano==408
tab SNPVs19 if pano==408
tab LDVs19 if pano==408
tab PCVs19 if pano==408
tab GrnVs19 if pano==408
tab BrexitVs19 if pano==408
tab UKIPVs19 if pano==408
tab OtherVs19 if pano==408

replace Fourth19=9 if pano==408

tab ConVs19 if pano==426
tab LabVs19 if pano==426
tab SNPVs19 if pano==426
tab LDVs19 if pano==426
tab PCVs19 if pano==426
tab GrnVs19 if pano==426
tab BrexitVs19 if pano==426
tab UKIPVs19 if pano==426
tab OtherVs19 if pano==426

replace Fourth19=3 if pano==426

tab ConVs19 if pano==482
tab LabVs19 if pano==482
tab SNPVs19 if pano==482
tab LDVs19 if pano==482
tab PCVs19 if pano==482
tab GrnVs19 if pano==482
tab BrexitVs19 if pano==482
tab UKIPVs19 if pano==482
tab OtherVs19 if pano==482

replace Fourth19=3 if pano==482

tab ConVs19 if pano==522
tab LabVs19 if pano==522
tab SNPVs19 if pano==522
tab LDVs19 if pano==522
tab PCVs19 if pano==522
tab GrnVs19 if pano==522
tab BrexitVs19 if pano==522
tab UKIPVs19 if pano==522
tab OtherVs19 if pano==522

replace Fourth19=8 if pano==522

tab ConVs19 if pano==576
tab LabVs19 if pano==576
tab SNPVs19 if pano==576
tab LDVs19 if pano==576
tab PCVs19 if pano==576
tab GrnVs19 if pano==576
tab BrexitVs19 if pano==576
tab UKIPVs19 if pano==576
tab OtherVs19 if pano==576

replace Fourth19=3 if pano==576

label values Fourth19 con_label
tab Fourth19

*** Fifth party indicator:

* coding empty indicator

gen Fifth19=.


tab pano if eff_parties19==5

* only 2 constituencies with 5 EPs, so these are coded manually:

tab ConVs19 if pano==29
tab LabVs19 if pano==29
tab SNPVs19 if pano==29
tab LDVs19 if pano==29
tab PCVs19 if pano==29
tab GrnVs19 if pano==29
tab BrexitVs19 if pano==29
tab UKIPVs19 if pano==29
tab OtherVs19 if pano==29

replace Fifth19=7 if pano==29

tab ConVs19 if pano==30
tab LabVs19 if pano==30
tab SNPVs19 if pano==30
tab LDVs19 if pano==30
tab PCVs19 if pano==30
tab GrnVs19 if pano==30
tab BrexitVs19 if pano==30
tab UKIPVs19 if pano==30
tab OtherVs19 if pano==30


replace Fifth19=7 if pano==30

*** Next, we code effective party identifier and effective party vote share variables:

tab eff_party_19_1
tab Winner19

** For the first effective party:
* identifier var

gen efp1_id_19=.
replace efp1_id_19=1 if eff_party_19_1==0 & Winner19==1
replace efp1_id_19=2 if eff_party_19_1==0 & Winner19==2
replace efp1_id_19=3 if eff_party_19_1==0 & Winner19==3
replace efp1_id_19=4 if eff_party_19_1==0 & Winner19==4
replace efp1_id_19=5 if eff_party_19_1==0 & Winner19==5
replace efp1_id_19=7 if eff_party_19_1==0 & Winner19==7

label values efp1_id_19 con_label
tab efp1_id_19

* vote share var:

gen efp1_VS_19=.
replace efp1_VS_19=ConVs19 if eff_party_19_1==0 & Winner19==1
replace efp1_VS_19=LabVs19 if eff_party_19_1==0 & Winner19==2
replace efp1_VS_19=LDVs19 if eff_party_19_1==0 & Winner19==3
replace efp1_VS_19=SNPVs19 if eff_party_19_1==0 & Winner19==4
replace efp1_VS_19=PCVs19 if eff_party_19_1==0 & Winner19==5
replace efp1_VS_19=UKIPVs19 if eff_party_19_1==0 & Winner19==6
replace efp1_VS_19=GrnVs19 if eff_party_19_1==0 & Winner19==7
tab efp1_VS_19

** For the second effective party:
* identifier var

tab eff_party_19_2
tab Second19

gen efp2_id_19=.
replace efp2_id_19=1 if eff_party_19_2==0 & Second19==1
replace efp2_id_19=2 if eff_party_19_2==0 & Second19==2
replace efp2_id_19=3 if eff_party_19_2==0 & Second19==3
replace efp2_id_19=4 if eff_party_19_2==0 & Second19==4
replace efp2_id_19=7 if eff_party_19_2==0 & Second19==7
replace efp2_id_19=8 if eff_party_19_2==0 & Second19==8
replace efp2_id_19=9 if eff_party_19_2==0 & Second19==9

label values efp2_id_19 con_label
tab efp2_id_19

* vote share var

gen efp2_VS_19=.
replace efp2_VS_19=ConVs19 if eff_party_19_2==0 & Second19==1
replace efp2_VS_19=LabVs19 if eff_party_19_2==0 & Second19==2
replace efp2_VS_19=LDVs19 if eff_party_19_2==0 & Second19==3
replace efp2_VS_19=SNPVs19 if eff_party_19_2==0 & Second19==4
replace efp2_VS_19=GrnVs19 if eff_party_19_2==0 & Second19==7
replace efp2_VS_19=OtherVs19 if eff_party_19_2==0 & Second19==8
replace efp2_VS_19=BrexitVs19 if eff_party_19_2==0 & Second19==9
tab efp2_VS_19

** For the third effective party:
* identifier var

tab eff_party_19_3
tab Third19

gen efp3_id_19=.
replace efp3_id_19=1 if eff_party_19_3==0 & Third19==1
replace efp3_id_19=2 if eff_party_19_3==0 & Third19==2
replace efp3_id_19=3 if eff_party_19_3==0 & Third19==3
replace efp3_id_19=5 if eff_party_19_3==0 & Third19==5
replace efp3_id_19=7 if eff_party_19_3==0 & Third19==7
replace efp3_id_19=8 if eff_party_19_3==0 & Third19==8
replace efp3_id_19=9 if eff_party_19_3==0 & Third19==9

label values efp3_id_19 con_label
tab efp3_id_19

* vote share var

gen efp3_VS_19=.
replace efp3_VS_19=ConVs19 if eff_party_19_3==0 & Third19==1
replace efp3_VS_19=LabVs19 if eff_party_19_3==0 & Third19==2
replace efp3_VS_19=LDVs19 if eff_party_19_3==0 & Third19==3
replace efp3_VS_19=PCVs19 if eff_party_19_3==0 & Third19==5
replace efp3_VS_19=GrnVs19 if eff_party_19_3==0 & Third19==7
replace efp3_VS_19=OtherVs19 if eff_party_19_3==0 & Third19==8
replace efp3_VS_19=BrexitVs19 if eff_party_19_3==0 & Third19==9
tab efp3_VS_19

** For the fourth effective party:
* identifier var
tab eff_party_19_4
tab Fourth19

gen efp4_id_19=.
replace efp4_id_19=2 if eff_party_19_4==0 & Fourth19==2
replace efp4_id_19=3 if eff_party_19_4==0 & Fourth19==3
replace efp4_id_19=5 if eff_party_19_4==0 & Fourth19==5
replace efp4_id_19=8 if eff_party_19_4==0 & Fourth19==8
replace efp4_id_19=9 if eff_party_19_4==0 & Fourth19==9
label values efp4_id_19 con_label
tab efp4_id_19

* vote share var 

gen efp4_VS_19=.
replace efp4_VS_19=LabVs19 if eff_party_19_4==0 & Fourth19==2
replace efp4_VS_19=LDVs19 if eff_party_19_4==0 & Fourth19==3
replace efp4_VS_19=PCVs19 if eff_party_19_4==0 & Fourth19==5
replace efp4_VS_19=OtherVs19 if eff_party_19_4==0 & Fourth19==8
replace efp4_VS_19=BrexitVs19 if eff_party_19_4==0 & Fourth19==9
tab efp4_VS_19

** For the first effective party:
* identifier var

tab eff_party_19_5
tab Fifth19

gen efp5_id_19=.
replace efp5_id_19=7 if eff_party_19_5==0 & Fifth19==7

* vote share var:

gen efp5_VS_19=.
replace efp5_VS_19=GrnVs19 if eff_party_19_5==0 & Fifth19==7


* Calculating competiveness index: 

gen CI1_19 = efp1_VS_19 if eff_parties19==1
gen CI2_19 = ((efp1_VS_19*efp2_VS_19)*(eff_parties19^eff_parties19)) if eff_parties19==2
gen CI3_19 = ((efp1_VS_19*efp2_VS_19*efp3_VS_19)*(eff_parties19^eff_parties19)) if eff_parties19==3
gen CI4_19 = ((efp1_VS_19*efp2_VS_19*efp3_VS_19*efp4_VS_19)*(eff_parties19^eff_parties19)) if eff_parties19==4

egen CI_19 = rowmax(CI1_19 CI2_19 CI3_19 CI4_19)

tab CI_19
summ CI_19

tab eff_parties19

* Calculating raw margin variable 

gen raw_margin=(Majority19/100)*TotalVote19

gen raw_margin_W19=round(raw_margin)

* Dummy for whether Brexit Party or UKIP was an effective party 

bysort pano: gen farright_ep_W19=0
bysort pano: replace farright_ep_W19=1 if efp1_id_19==9|efp2_id_19==9|efp3_id_19==9|efp4_id_19==9|efp5_id_19==9

tab farright_ep

* Duumy for whether the constituency was part of the Brexit-Con pact

tab Winner17

bysort pano: gen BrexitCon_pact=0
bysort pano: replace BrexitCon_pact=1 if Winner17==1

keep CI_19 Winner19 Second19 Third19 Fourth19 ConVs19 LabVs19 LDVs19 SNPVs19 PCVs19 UKIPVs19 GrnVs19 pano ConstituencyName ONSConstID eff_parties19 efp1_id_19 efp2_id_19 efp3_id_19 efp4_id_19 efp5_id_19 raw_margin_W19 farright_ep_W19 BrexitCon_pact Electorate19

rename pano panoW19

save CI_2019_v2.dta, replace

use CI_2019_v2.dta, clear

summ CI_19

**# 2. Coding CI index for 2017

* load in the constituency-level election result data:

use BES-2019-General-Election-results-file-v1.1.dta, clear

save CI_2017_v1.dta, replace

* begin by dropping the speaker's constituency (no contest from the major parties here)

numlabel, add

tab Winner17
drop if Winner17==9


*** next, we calculate the number of effective parties per constituency. 

* recode the vote share variables into decimals:

gen ConVs17=Con17/100
gen LabVs17=Lab17/100
gen LDVs17=LD17/100
gen UKIPVs17=UKIP17/100
gen GrnVs17=Green17/100
gen SNPVs17=SNP17/100
gen PCVs17=PC17/100
gen OtherVs17=Other17/100

* recode missing as 0s

recode ConVs17 LabVs17 PCVs17 SNPVs17 OtherVs17 GrnVs17 UKIPVs17 LDVs17 (.=0)

* calculate number of effective parties per constituency

bysort ConstituencyName: gen eff_part_17 = 1/((ConVs17)^2 + (LabVs17)^2 + (LDVs17)^2 + (UKIPVs17)^2 + (GrnVs17)^2 + (SNPVs17)^2 + (PCVs17)^2 + (OtherVs17)^2)

tab eff_part_17

* now round the effective party scores so that the variable has only integer values:

gen eff_parties17=.
replace eff_parties17 = 1 if eff_part_17<1.5
replace eff_parties17 = 2 if eff_part_17>=1.5 & eff_part_17<2.5 
replace eff_parties17 = 3 if ((eff_part_17>=2.5) & (eff_part_17<3.5))
replace eff_parties17 = 4 if eff_part_17>3.5
tab eff_parties17

** the next step is to calculate the effective party vote shares in each constituency. 

* begin by coding empty variables for each: 

gen eff_party_17_1=0
gen eff_party_17_2=0 if (eff_parties17==2)|(eff_parties17==3)|(eff_parties17==4)
gen eff_party_17_3=0 if (eff_parties17==3)|(eff_parties17==4)
gen eff_party_17_4=0 if eff_parties17==4

* The next step is to determine which party finished second/third/fourth/fifth in each constituency. 
* For this second party indicator, the parties are coded as follows. 1 - Con; 2 - Lab; 3 - LD; 4 - SNP; 5 - PC; 6 - UKIP; 7 - Grn; 8 - Other; 9 - Brexit.

gen Second17=.

* For those constituencies in which Labour was winner: 

replace Second17=1 if ((Winner17==2) & (ConVs17>LDVs17 & ConVs17>UKIPVs17 & ConVs17>GrnVs17 & ConVs17>SNPVs17 & ConVs17>PCVs17 & ConVs17>OtherVs17))
replace Second17=3 if ((Winner17==2) & (LDVs17>ConVs17 & LDVs17>UKIPVs17 & LDVs17>GrnVs17 & LDVs17>SNPVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17))
replace Second17=4 if ((Winner17==2) & (SNPVs17>ConVs17 & SNPVs17>LDVs17 & SNPVs17>UKIPVs17  & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Second17=5 if ((Winner17==2) & (PCVs17>ConVs17 & PCVs17>LDVs17 & PCVs17>UKIPVs17  & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Second17=6 if ((Winner17==2) & (UKIPVs17>ConVs17 & UKIPVs17>LDVs17 & UKIPVs17>GrnVs17 & UKIPVs17>SNPVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Second17=7 if ((Winner17==2) & (GrnVs17>ConVs17 & GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>SNPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Second17=8 if ((Winner17==2) & (OtherVs17>ConVs17 & OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17 & OtherVs17>SNPVs17))

* For those constituencies in which the Conservatives were winners:

replace Second17=2 if ((Winner17==1) & (LabVs17>LDVs17 & LabVs17>UKIPVs17 & LabVs17>GrnVs17 & LabVs17>SNPVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17))
replace Second17=3 if ((Winner17==1) & (LDVs17>LabVs17 & LDVs17>UKIPVs17 & LDVs17>GrnVs17 & LDVs17>SNPVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17))
replace Second17=4 if ((Winner17==1) & (SNPVs17>LabVs17 & SNPVs17>LDVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Second17=5 if ((Winner17==1) & (PCVs17>LabVs17 & PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Second17=6 if ((Winner17==1) & (UKIPVs17>LabVs17 & UKIPVs17>LDVs17 & UKIPVs17>GrnVs17 & UKIPVs17>SNPVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Second17=7 if ((Winner17==1) & (GrnVs17>LabVs17 & GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>SNPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Second17=8 if ((Winner17==1) & (OtherVs17>LabVs17 & OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17))

* LD as winner:
replace Second17=2 if ((Winner17==3) & (LabVs17>ConVs17 & LabVs17>UKIPVs17 & LabVs17>GrnVs17 & LabVs17>SNPVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17))
replace Second17=1 if ((Winner17==3) & (ConVs17>LabVs17 & ConVs17>UKIPVs17 & ConVs17>GrnVs17 & ConVs17>SNPVs17 & ConVs17>PCVs17 & ConVs17>OtherVs17))
replace Second17=4 if ((Winner17==3) & (SNPVs17>LabVs17 & SNPVs17>ConVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Second17=5 if ((Winner17==3) & (PCVs17>LabVs17 & PCVs17>ConVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Second17=6 if ((Winner17==3) & (UKIPVs17>LabVs17 & UKIPVs17>ConVs17 & UKIPVs17>GrnVs17 & UKIPVs17>SNPVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Second17=7 if ((Winner17==3) & (GrnVs17>LabVs17 & GrnVs17>ConVs17 & GrnVs17>UKIPVs17 & GrnVs17>SNPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Second17=8 if ((Winner17==3) & (OtherVs17>LabVs17 & OtherVs17>ConVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17))

* SNP as winner:
replace Second17=2 if ((Winner17==4) & (LabVs17>ConVs17 & LabVs17>UKIPVs17 & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17 & LabVs17>LDVs17))
replace Second17=1 if ((Winner17==4) & (ConVs17>LabVs17 & ConVs17>UKIPVs17 & ConVs17>GrnVs17 & ConVs17>OtherVs17 & ConVs17>LDVs17))
replace Second17=3 if ((Winner17==4) & (LDVs17>LabVs17 & LDVs17>UKIPVs17 & LDVs17>GrnVs17 & LDVs17>OtherVs17 & LDVs17>ConVs17))
replace Second17=6 if ((Winner17==4) & (UKIPVs17>LabVs17 & UKIPVs17>ConVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Second17=7 if ((Winner17==4) & (GrnVs17>LabVs17 & GrnVs17>ConVs17 & GrnVs17>UKIPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Second17=8 if ((Winner17==4) & (OtherVs17>LabVs17 & OtherVs17>ConVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17))


* PC as winner:

tab PCVs17 pano if Winner17==5
tab LabVs17 pano if Winner17==5
tab ConVs17 pano if Winner17==5
tab LDVs17 pano if Winner17==5

replace Second17=2 if pano==16
replace Second17=2 if pano==131
replace Second17=3 if pano==135
replace Second17=1 if pano==217

* Green as winner: 
tab LabVs17 pano if Winner17==7
tab ConVs17 pano if Winner17==7

replace Second17=2 if pano==98

* creating labels for the party position variables:

label define con_label 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other"
label values Second17 con_label
tab Second17

save CI_2019_v1.dta, replace

* Coding an empty third party indicator 

gen Third17=.

* Conservatives as winners:

replace Third17=3 if ((Winner17==1) & (Second17==2) & (LDVs17>UKIPVs17 & LDVs17>GrnVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17 & LDVs17>SNPVs17))
replace Third17=4 if ((Winner17==1) & (Second17==2) & (SNPVs17>LDVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Third17=5 if ((Winner17==1) & (Second17==2) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==1) & (Second17==2) & (UKIPVs17>LDVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==1) & (Second17==2) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17  & GrnVs17>PCVs17 & GrnVs17>OtherVs17 & GrnVs17>SNPVs17))
replace Third17=8 if ((Winner17==1) & (Second17==2) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17 & OtherVs17>SNPVs17))

replace Third17=2 if ((Winner17==1) & (Second17==3) & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17 & LabVs17>SNPVs17))
replace Third17=4 if ((Winner17==1) & (Second17==3) & (SNPVs17>LabVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Third17=5 if ((Winner17==1) & (Second17==3) & (PCVs17>LabVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==1) & (Second17==3) & (UKIPVs17>LabVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==1) & (Second17==3) & (GrnVs17>LabVs17 & GrnVs17>UKIPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17 & GrnVs17>SNPVs17))
replace Third17=8 if ((Winner17==1) & (Second17==3) & (OtherVs17>LabVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17 & OtherVs17>SNPVs17))

replace Third17=3 if ((Winner17==1) & (Second17==4) & (LDVs17>UKIPVs17 & LDVs17>LabVs17 & LDVs17>GrnVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17))
replace Third17=2 if ((Winner17==1) & (Second17==4) & (LabVs17>LDVs17 & LabVs17>UKIPVs17 & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17))
replace Third17=5 if ((Winner17==1) & (Second17==4) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>LabVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==1) & (Second17==4) & (UKIPVs17>LDVs17 & UKIPVs17>LabVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Third17=7 if ((Winner17==1) & (Second17==4) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>LabVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Third17=8 if ((Winner17==1) & (Second17==4) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>LabVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17))

replace Third17=3 if ((Winner17==1) & (Second17==5) & (LDVs17>UKIPVs17 & LDVs17>LabVs17 & LDVs17>GrnVs17 & LDVs17>OtherVs17 & LDVs17>SNPVs17))
replace Third17=2 if ((Winner17==1) & (Second17==5) & (LabVs17>LDVs17 & LabVs17>UKIPVs17 & LabVs17>GrnVs17 & LabVs17>OtherVs17 & LabVs17>SNPVs17))
replace Third17=6 if ((Winner17==1) & (Second17==5) & (UKIPVs17>LDVs17 & UKIPVs17>LabVs17 & UKIPVs17>GrnVs17 & UKIPVs17>OtherVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==1) & (Second17==5) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>LabVs17 & GrnVs17>OtherVs17 & GrnVs17>SNPVs17))
replace Third17=8 if ((Winner17==1) & (Second17==5) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>LabVs17 & OtherVs17>GrnVs17 & OtherVs17>SNPVs17))

replace Third17=3 if ((Winner17==1) & (Second17==8) & (LDVs17>UKIPVs17 & LDVs17>LabVs17 & LDVs17>PCVs17 & LDVs17>GrnVs17 & LDVs17>SNPVs17))
replace Third17=2 if ((Winner17==1) & (Second17==8) & (LabVs17>LDVs17 & LabVs17>UKIPVs17  & LabVs17>PCVs17 & LabVs17>GrnVs17& LabVs17>SNPVs17))
replace Third17=4 if ((Winner17==1) & (Second17==8) & (SNPVs17>LDVs17 & SNPVs17>UKIPVs17 & SNPVs17>LabVs17 & SNPVs17>GrnVs17))
replace Third17=5 if ((Winner17==1) & (Second17==8) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>LabVs17 & PCVs17>GrnVs17))
replace Third17=6 if ((Winner17==1) & (Second17==8) & (UKIPVs17>LDVs17 & UKIPVs17>LabVs17 & UKIPVs17>PCVs17 & UKIPVs17>GrnVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==1) & (Second17==8) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>LabVs17 & GrnVs17>PCVs17 & GrnVs17>SNPVs17))

* Lab as winners:

replace Third17=3 if ((Winner17==2) & (Second17==1) & (LDVs17>UKIPVs17 & LDVs17>GrnVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17 & LDVs17>SNPVs17))
replace Third17=4 if ((Winner17==2) & (Second17==1) & (SNPVs17>LDVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Third17=5 if ((Winner17==2) & (Second17==1) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==2) & (Second17==1) & (UKIPVs17>LDVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==2) & (Second17==1) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17  & GrnVs17>PCVs17 & GrnVs17>OtherVs17 & GrnVs17>SNPVs17))
replace Third17=8 if ((Winner17==2) & (Second17==1) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17 & OtherVs17>SNPVs17))

replace Third17=1 if ((Winner17==2) & (Second17==3) & (ConVs17>UKIPVs17  & ConVs17>GrnVs17 & ConVs17>PCVs17 & ConVs17>OtherVs17 & ConVs17>SNPVs17))
replace Third17=4 if ((Winner17==2) & (Second17==3) & (SNPVs17>ConVs17 & SNPVs17>UKIPVs17 & SNPVs17>GrnVs17 & SNPVs17>OtherVs17))
replace Third17=5 if ((Winner17==2) & (Second17==3) & (PCVs17>ConVs17 & PCVs17>UKIPVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==2) & (Second17==3) & (UKIPVs17>ConVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==2) & (Second17==3) & (GrnVs17>ConVs17 & GrnVs17>UKIPVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17 & GrnVs17>SNPVs17))
replace Third17=8 if ((Winner17==2) & (Second17==3) & (OtherVs17>ConVs17 & OtherVs17>UKIPVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17 & OtherVs17>SNPVs17))

replace Third17=3 if ((Winner17==2) & (Second17==4) & (LDVs17>UKIPVs17 & LDVs17>ConVs17 & LDVs17>GrnVs17 & LDVs17>PCVs17 & LDVs17>OtherVs17))
replace Third17=1 if ((Winner17==2) & (Second17==4) & (ConVs17>LDVs17 & ConVs17>UKIPVs17 & ConVs17>GrnVs17 & ConVs17>PCVs17 & ConVs17>OtherVs17))
replace Third17=5 if ((Winner17==2) & (Second17==4) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>ConVs17 & PCVs17>GrnVs17 & PCVs17>OtherVs17))
replace Third17=6 if ((Winner17==2) & (Second17==4) & (UKIPVs17>LDVs17 & UKIPVs17>ConVs17 & UKIPVs17>GrnVs17 & UKIPVs17>PCVs17 & UKIPVs17>OtherVs17))
replace Third17=7 if ((Winner17==2) & (Second17==4) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>ConVs17 & GrnVs17>PCVs17 & GrnVs17>OtherVs17))
replace Third17=8 if ((Winner17==2) & (Second17==4) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>ConVs17 & OtherVs17>GrnVs17 & OtherVs17>PCVs17))

replace Third17=3 if ((Winner17==2) & (Second17==5) & (LDVs17>UKIPVs17 & LDVs17>ConVs17 & LDVs17>GrnVs17 & LDVs17>OtherVs17))
replace Third17=1 if ((Winner17==2) & (Second17==5) & (ConVs17>LDVs17 & ConVs17>UKIPVs17 & ConVs17>GrnVs17 & ConVs17>OtherVs17))
replace Third17=6 if ((Winner17==2) & (Second17==5) & (UKIPVs17>LDVs17 & UKIPVs17>ConVs17 & UKIPVs17>GrnVs17 & UKIPVs17>OtherVs17))
replace Third17=7 if ((Winner17==2) & (Second17==5) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>ConVs17 & GrnVs17>OtherVs17))
replace Third17=8 if ((Winner17==2) & (Second17==5) & (OtherVs17>LDVs17 & OtherVs17>UKIPVs17 & OtherVs17>ConVs17 & OtherVs17>GrnVs17))

replace Third17=3 if ((Winner17==2) & (Second17==8) & (LDVs17>UKIPVs17 & LDVs17>ConVs17 & LDVs17>PCVs17 & LDVs17>GrnVs17 & LDVs17>SNPVs17))
replace Third17=1 if ((Winner17==2) & (Second17==8) & (ConVs17>LDVs17 & ConVs17>UKIPVs17  & ConVs17>PCVs17 & ConVs17>GrnVs17& ConVs17>SNPVs17))
replace Third17=4 if ((Winner17==2) & (Second17==8) & (SNPVs17>LDVs17 & SNPVs17>UKIPVs17 & SNPVs17>ConVs17 & SNPVs17>GrnVs17))
replace Third17=5 if ((Winner17==2) & (Second17==8) & (PCVs17>LDVs17 & PCVs17>UKIPVs17 & PCVs17>ConVs17 & PCVs17>GrnVs17))
replace Third17=6 if ((Winner17==2) & (Second17==8) & (UKIPVs17>LDVs17 & UKIPVs17>ConVs17 & UKIPVs17>PCVs17 & UKIPVs17>GrnVs17 & UKIPVs17>SNPVs17))
replace Third17=7 if ((Winner17==2) & (Second17==8) & (GrnVs17>LDVs17 & GrnVs17>UKIPVs17 & GrnVs17>ConVs17 & GrnVs17>PCVs17 & GrnVs17>SNPVs17))

* LD. Only 2nd-placed parties are Tories and SNP:
 
tab Second17 if Winner17==3

replace Third17=2 if Winner17==3 & Second17==1 & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17 & LabVs17>SNPVs17)
replace Third17=2 if Winner17==3 & Second17==4 & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17 & LabVs17>ConVs17)
replace Third17=1 if Winner17==3 & Second17==4 & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>PCVs17 & LabVs17>OtherVs17 & LabVs17>ConVs17)
replace Third17=1 if Winner17==3 & Second17==4 & (ConVs17>UKIPVs17  & ConVs17>GrnVs17 & ConVs17>PCVs17 & ConVs17>OtherVs17 & ConVs17>LabVs17)


* SNP. Only 2nd-placd parties are Tories, Labour and Lib Dem:
tab Second17 if Winner17==4

replace Third17=2 if Winner17==4 & Second17==1 & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>OtherVs17 & LabVs17>LDVs17)
replace Third17=3 if Winner17==4 & Second17==1 & (LDVs17>UKIPVs17  & LDVs17>GrnVs17 & LDVs17>OtherVs17 & LDVs17>LabVs17)

replace Third17=1 if Winner17==4 & Second17==2 & (ConVs17>UKIPVs17  & ConVs17>GrnVs17 & ConVs17>OtherVs17 & ConVs17>LDVs17)

replace Third17=2 if Winner17==4 & Second17==3 & (LabVs17>UKIPVs17  & LabVs17>GrnVs17 & LabVs17>OtherVs17 & LabVs17>ConVs17)
replace Third17=1 if Winner17==4 & Second17==3 & (ConVs17>UKIPVs17  & ConVs17>GrnVs17 & ConVs17>OtherVs17 & ConVs17>LabVs17)

* PC: 

tab PCVs17 pano if Winner17==5
tab LabVs17 pano if Winner17==5
tab ConVs17 pano if Winner17==5
tab LDVs17 pano if Winner17==5

replace Third17=1 if pano==16
replace Third17=1 if pano==131
replace Third17=2 if pano==135
replace Third17=2 if pano==217

* Greens: 
tab LabVs17 pano if Winner17==7
tab ConVs17 pano if Winner17==7

replace Third17=1 if pano==98

tab Winner17
tab Third17

label values Third17 con_label
tab Third17

*** Fourth party indicator:

* coding an empty indicator 

gen Fourth17=.

* coding this manually, due to the small number of constituencies with 4 effective parties:

tab eff_parties17
tab pano if eff_parties17==4

tab Winner17 if pano==115
tab Second17 if pano==115
tab Third17 if pano==115

tab LDVs17 if pano==115
tab SNPVs17 if pano==115
tab ConVs17 if pano==115
tab LabVs17 if pano==115
tab UKIPVs17 if pano==115
tab GrnVs17 if pano==115

replace Fourth17=2 if pano==115

tab Winner17 if pano==135
tab Second17 if pano==135
tab Third17 if pano==135

tab LDVs17 if pano==135
tab ConVs17 if pano==135
tab LabVs17 if pano==135
tab UKIPVs17 if pano==135
tab GrnVs17 if pano==135

replace Fourth17=1 if pano==135

tab Winner17 if pano==232
tab Second17 if pano==232
tab Third17 if pano==232

tab LDVs17 if pano==232
tab ConVs17 if pano==232
tab LabVs17 if pano==232
tab UKIPVs17 if pano==232
tab GrnVs17 if pano==232

replace Fourth17=2 if pano==232

tab Winner17 if pano==250
tab Second17 if pano==250
tab Third17 if pano==250

tab LDVs17 if pano==250
tab ConVs17 if pano==250
tab LabVs17 if pano==250
tab UKIPVs17 if pano==250
tab GrnVs17 if pano==250

replace Fourth17=2 if pano==250

tab Winner17 if pano==479
tab Second17 if pano==479
tab Third17 if pano==479

tab LDVs17 if pano==479
tab ConVs17 if pano==479
tab LabVs17 if pano==479
tab UKIPVs17 if pano==479
tab GrnVs17 if pano==479

replace Fourth17=2 if pano==479


label values Fourth17 con_label
tab Fourth17

*** Next, we code effective party identifier and effective party vote share variables:

tab eff_party_17_1
tab Winner17

** For the first effective party:
* identifier var

gen efp1_id_17=.
replace efp1_id_17=1 if eff_party_17_1==0 & Winner17==1
replace efp1_id_17=2 if eff_party_17_1==0 & Winner17==2
replace efp1_id_17=3 if eff_party_17_1==0 & Winner17==3
replace efp1_id_17=4 if eff_party_17_1==0 & Winner17==4
replace efp1_id_17=5 if eff_party_17_1==0 & Winner17==5
replace efp1_id_17=7 if eff_party_17_1==0 & Winner17==7

label values efp1_id_17 con_label
tab efp1_id_17

* vote share var:

gen efp1_VS_17=.
replace efp1_VS_17=ConVs17 if eff_party_17_1==0 & Winner17==1
replace efp1_VS_17=LabVs17 if eff_party_17_1==0 & Winner17==2
replace efp1_VS_17=LDVs17 if eff_party_17_1==0 & Winner17==3
replace efp1_VS_17=SNPVs17 if eff_party_17_1==0 & Winner17==4
replace efp1_VS_17=PCVs17 if eff_party_17_1==0 & Winner17==5
replace efp1_VS_17=GrnVs17 if eff_party_17_1==0 & Winner17==7
tab efp1_VS_17

** For the second effective party:
* identifier var

tab eff_party_17_2
tab Second17

gen efp2_id_17=.
replace efp2_id_17=1 if eff_party_17_2==0 & Second17==1
replace efp2_id_17=2 if eff_party_17_2==0 & Second17==2
replace efp2_id_17=3 if eff_party_17_2==0 & Second17==3
replace efp2_id_17=4 if eff_party_17_2==0 & Second17==4
replace efp2_id_17=5 if eff_party_17_2==0 & Second17==5
replace efp2_id_17=7 if eff_party_17_2==0 & Second17==7
replace efp2_id_17=8 if eff_party_17_2==0 & Second17==8

label values efp2_id_17 con_label
tab efp2_id_17

* vote share var

gen efp2_VS_17=.
replace efp2_VS_17=ConVs17 if eff_party_17_2==0 & Second17==1
replace efp2_VS_17=LabVs17 if eff_party_17_2==0 & Second17==2
replace efp2_VS_17=LDVs17 if eff_party_17_2==0 & Second17==3
replace efp2_VS_17=SNPVs17 if eff_party_17_2==0 & Second17==4
replace efp2_VS_17=PCVs17 if eff_party_17_2==0 & Second17==5
replace efp2_VS_17=GrnVs17 if eff_party_17_2==0 & Second17==7
replace efp2_VS_17=OtherVs17 if eff_party_17_2==0 & Second17==8
tab efp2_VS_17

** For the third effective party:
* identifier var

tab eff_party_17_3
tab Third17

gen efp3_id_17=.
replace efp3_id_17=1 if eff_party_17_3==0 & Third17==1
replace efp3_id_17=2 if eff_party_17_3==0 & Third17==2
replace efp3_id_17=3 if eff_party_17_3==0 & Third17==3
replace efp3_id_17=5 if eff_party_17_3==0 & Third17==5
replace efp3_id_17=6 if eff_party_17_3==0 & Third17==6
replace efp3_id_17=7 if eff_party_17_3==0 & Third17==7
replace efp3_id_17=8 if eff_party_17_3==0 & Third17==8

label values efp3_id_17 con_label
tab efp3_id_17

* vote share var

gen efp3_VS_17=.
replace efp3_VS_17=ConVs17 if eff_party_17_3==0 & Third17==1
replace efp3_VS_17=LabVs17 if eff_party_17_3==0 & Third17==2
replace efp3_VS_17=LDVs17 if eff_party_17_3==0 & Third17==3
replace efp3_VS_17=PCVs17 if eff_party_17_3==0 & Third17==5
replace efp3_VS_17=UKIPVs17 if eff_party_17_3==0 & Third17==6
replace efp3_VS_17=GrnVs17 if eff_party_17_3==0 & Third17==7
replace efp3_VS_17=OtherVs17 if eff_party_17_3==0 & Third17==8
tab efp3_VS_17

** For the fourth effective party:
* identifier var

tab eff_party_17_4
tab Fourth17

gen efp4_id_17=.
replace efp4_id_17=1 if eff_party_17_4==0 & Fourth17==1
replace efp4_id_17=2 if eff_party_17_4==0 & Fourth17==2
label values efp4_id_17 con_label
tab efp4_id_17

* vote share var 

gen efp4_VS_17=.
replace efp4_VS_17=ConVs17 if eff_party_17_4==0 & Fourth17==1
replace efp4_VS_17=LabVs17 if eff_party_17_4==0 & Fourth17==2
tab efp4_VS_17

* Number of effective parties in each race:

tab eff_parties17

*Total frequency at which a party was effective: 

numlabel, add

tab efp1_id_17
tab efp2_id_17
tab efp3_id_17
tab efp4_id_17

* Calculating competiveness index: 

gen CI1_17 = efp1_VS_17 if eff_parties17==1
gen CI2_17 = ((efp1_VS_17*efp2_VS_17)*(eff_parties17^eff_parties17)) if eff_parties17==2
gen CI3_17 = ((efp1_VS_17*efp2_VS_17*efp3_VS_17)*(eff_parties17^eff_parties17)) if eff_parties17==3
gen CI4_17 = ((efp1_VS_17*efp2_VS_17*efp3_VS_17*efp4_VS_17)*(eff_parties17^eff_parties17)) if eff_parties17==4

egen CI_17 = rowmax(CI1_17 CI2_17 CI3_17 CI4_17)

tab CI_17
summ CI_17

tab eff_parties17

* Calculating raw margin variable 

gen raw_margin=(Majority17/100)*TotalVote17

gen raw_margin_W13=round(raw_margin)

* Dummy for whether Brexit Party or UKIP was an effective party 

bysort pano: gen farright_ep_W13=0
bysort pano: replace farright_ep_W13=1 if efp1_id_17==6|efp2_id_17==6|efp3_id_17==6|efp4_id_17==6

tab farright_ep_W13

keep CI_17 Winner17 Second17 Third17 Fourth17 ConVs17 LabVs17 LDVs17 SNPVs17 PCVs17 UKIPVs17 GrnVs17 pano ConstituencyName ONSConstID eff_parties17 efp1_id_17 efp2_id_17 efp3_id_17 efp4_id_17 raw_margin_W13 farright_ep_W13 Electorate17 efp1_VS_17 efp2_VS_17 efp3_VS_17 efp4_VS_17

rename pano panoW13

save CI_2017_v2.dta, replace

use CI_2017_v2.dta, clear

summ CI_17

**# 3. Coding CI index for 2015

* load in the constituency-level election result data:

use BES-2019-General-Election-results-file-v1.1.dta, clear

save CI_2015_v1.dta, replace

* begin by dropping the speaker's constituency (no contest from the major parties here)

numlabel, add 

tab Winner15
drop if Winner15==9

* recode missing as 0s

recode Lab15 Con15 LD15 Green15 Other15 UKIP15 SNP15 PC15 (.=0)

*** next, we calculate the number of effective parties per constituency. 

* recode the vote share variables into decimals:

gen ConVs15=Con15/100
gen LabVs15=Lab15/100
gen LDVs15=LD15/100
gen UKIPVs15=UKIP15/100
gen GrnVs15=Green15/100
gen SNPVs15=SNP15/100
gen PCVs15=PC15/100
gen OtherVs15=Other15/100

* calculate number of effective parties per constituency

bysort ConstituencyName: gen eff_part_15 = 1/((ConVs15)^2 + (LabVs15)^2 + (LDVs15)^2 + (UKIPVs15)^2 + (GrnVs15)^2 + (SNPVs15)^2 + (PCVs15)^2 + (OtherVs15)^2)

tab eff_part_15

* now round the effective party scores so that the variable has only integer values:

gen eff_parties15=.
replace eff_parties15 = 1 if eff_part_15<1.5
replace eff_parties15 = 2 if eff_part_15>=1.5 & eff_part_15<2.5 
replace eff_parties15 = 3 if ((eff_part_15>=2.5) & (eff_part_15<3.5))
replace eff_parties15 = 4 if eff_part_15>3.5
tab eff_parties15

** the next step is to calculate the effective party vote shares in each constituency. 

* begin by coding empty variables for each: 

gen eff_party_15_1=0 
gen eff_party_15_2=0 if (eff_parties15==2)|(eff_parties15==3)|(eff_parties15==4)
gen eff_party_15_3=0 if (eff_parties15==3)|(eff_parties15==4)
gen eff_party_15_4=0 if eff_parties15==4

* The next step is to determine which party finished second/third/fourth/fifth in each constituency. 
* For this second party indicator, the parties are coded as follows. 1 - Con; 2 - Lab; 3 - LD; 4 - SNP; 5 - PC; 6 - UKIP; 7 - Grn; 8 - Other; 9 - Brexit.

* coding second party indicator

gen Second15=.

tab Winner15

tab ConVs if Winner15==7
tab LabVs if Winner15==7

* Con as 2nd:

* ENGLAND:

replace Second15=1 if ((Winner15==2) & (ConVs15>LDVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15)) & Country==1
replace Second15=1 if ((Winner15==3) & (ConVs15>LabVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15)) & Country==1
replace Second15=1 if ((Winner15==6) & (ConVs15>LabVs15 & ConVs15>GrnVs15 & ConVs15>LDVs15 & ConVs15>OtherVs15)) & Country==1

* SCOTLAND:
replace Second15=1 if ((Winner15==2) & (ConVs15>LDVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15)) & Country==2
replace Second15=1 if ((Winner15==3) & (ConVs15>LabVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15)) & Country==2
replace Second15=1 if ((Winner15==4) & (ConVs15>LabVs15 & ConVs15>GrnVs15 & ConVs15>LDVs15 & ConVs15>OtherVs15 & ConVs15>UKIPVs15)) & Country==2

* WALES:
replace Second15=1 if ((Winner15==2) & (ConVs15>LDVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>PCVs15)) & Country==3
replace Second15=1 if ((Winner15==3) & (ConVs15>LabVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>PCVs15)) & Country==3
replace Second15=1 if ((Winner15==5) & (ConVs15>LabVs15 & ConVs15>GrnVs15 & ConVs15>LDVs15 & ConVs15>OtherVs15 & ConVs15>UKIPVs15)) & Country==3

* Labour as 2nd: 

* ENGLAND
replace Second15=2 if ((Winner15==1) & (LabVs15>LDVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15)) & Country==1
replace Second15=2 if ((Winner15==3) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15)) & Country==1
replace Second15=2 if ((Winner15==7) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>LDVs15 & LabVs15>OtherVs15)) & Country==1
replace Second15=2 if ((Winner15==6) & (LabVs15>ConVs15 & LabVs15>GrnVs15 & LabVs15>LDVs15 & LabVs15>OtherVs15)) & Country==1

* SCOTLAND: 
replace Second15=2 if ((Winner15==1) & (LabVs15>LDVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15 & LabVs15>SNPVs15)) & Country==2
replace Second15=2 if ((Winner15==3) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15 & LabVs15>SNPVs15)) & Country==2
replace Second15=2 if ((Winner15==4) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>LDVs15 & LabVs15>OtherVs15 & LabVs15>GrnVs15)) & Country==2

* WALES:
replace Second15=2 if ((Winner15==1) & (LabVs15>LDVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15 & LabVs15>PCVs15)) & Country==3
replace Second15=2 if ((Winner15==3) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>OtherVs15 & LabVs15>PCVs15)) & Country==3
replace Second15=2 if ((Winner15==5) & (LabVs15>ConVs15 & LabVs15>UKIPVs15 & LabVs15>LDVs15 & LabVs15>OtherVs15 & LabVs15>GrnVs15)) & Country==3

* LD as 2nd:

* ENGLAND:
replace Second15=3 if ((Winner15==1) & (LDVs15>LabVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15)) & Country==1
replace Second15=3 if ((Winner15==2) & (LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15)) & Country==1

* SCOTLAND:
replace Second15=3 if ((Winner15==1) & (LDVs15>LabVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15)) & Country==2
replace Second15=3 if ((Winner15==2) & (LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15)) & Country==2
replace Second15=3 if ((Winner15==4) & (LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15)) & Country==2

* WALES:
replace Second15=3 if ((Winner15==1) & (LDVs15>LabVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>PCVs15)) & Country==3
replace Second15=3 if ((Winner15==2) & (LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>PCVs15)) & Country==3
replace Second15=3 if ((Winner15==5) & (LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15)) & Country==3

* SNP as 2nd:
replace Second15=4 if ((Winner15==1) & (SNPVs15>LabVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15 & SNPVs15>LDVs15)) & Country==2
replace Second15=4 if ((Winner15==2) & (SNPVs15>ConVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15 & SNPVs15>LDVs15)) & Country==2
replace Second15=4 if ((Winner15==3) & (SNPVs15>LabVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15 & SNPVs15>ConVs15)) & Country==2

* PC as 2nd:
replace Second15=5 if ((Winner15==1) & (PCVs15>LabVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15 & PCVs15>LDVs15)) & Country==3
replace Second15=5 if ((Winner15==2) & (PCVs15>ConVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15 & PCVs15>LDVs15)) & Country==3
replace Second15=5 if ((Winner15==3) & (PCVs15>LabVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15 & PCVs15>ConVs15)) & Country==3

* UKIP as 2nd:
* ENGLAND:
replace Second15=6 if ((Winner15==1) & (UKIPVs15>LabVs15 & UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15)) & Country==1
replace Second15=6 if ((Winner15==2) & (UKIPVs15>ConVs15 & UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15)) & Country==1
replace Second15=6 if ((Winner15==3) & (UKIPVs15>LabVs15 & UKIPVs15>ConVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15)) & Country==1

* WALES:
replace Second15=6 if ((Winner15==1) & (UKIPVs15>LabVs15 & UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15 & UKIPVs15>PCVs15)) & Country==3
replace Second15=6 if ((Winner15==2) & (UKIPVs15>ConVs15 & UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15 & UKIPVs15>PCVs15)) & Country==3


* Green as 2nd: 
replace Second15=7 if ((Winner15==1) & (GrnVs15>LabVs15 & GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>OtherVs15)) & Country==1
replace Second15=7 if ((Winner15==2) & (GrnVs15>ConVs15 & GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>OtherVs15)) & Country==1
replace Second15=7 if ((Winner15==3) & (GrnVs15>LabVs15 & GrnVs15>ConVs15 & GrnVs15>UKIPVs15 & GrnVs15>OtherVs15)) & Country==1

* Other as 2nd: 
replace Second15=8 if ((Winner15==1) & (OtherVs15>LabVs15 & OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15)) & Country==1
replace Second15=8 if ((Winner15==2) & (OtherVs15>ConVs15 & OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15)) & Country==1
replace Second15=8 if ((Winner15==3) & (OtherVs15>LabVs15 & OtherVs15>ConVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15)) & Country==1

tab Second15 Country
tab Winner15 Country

* creating labels for the party position variables:

label define con_label 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other"
label values Second15 con_label
tab Second15

save CI_2015_v1.dta, replace

* Third party vote share. 

* Coding an empty third party indicator 

gen Third15=.

* Conservatives as winner:

replace Third15=3 if ((Winner15==1) & (Second15==2) & (LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>PCVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15))
replace Third15=4 if ((Winner15==1) & (Second15==2) & (SNPVs15>LDVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==1) & (Second15==2) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==1) & (Second15==2) & (UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==1) & (Second15==2) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15  & GrnVs15>PCVs15 & GrnVs15>OtherVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==1) & (Second15==2) & (OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15 & OtherVs15>SNPVs15))

replace Third15=2 if ((Winner15==1) & (Second15==3) & (LabVs15>UKIPVs15  & LabVs15>GrnVs15 & LabVs15>PCVs15 & LabVs15>OtherVs15 & LabVs15>SNPVs15))
replace Third15=4 if ((Winner15==1) & (Second15==3) & (SNPVs15>LabVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==1) & (Second15==3) & (PCVs15>LabVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==1) & (Second15==3) & (UKIPVs15>LabVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==1) & (Second15==3) & (GrnVs15>LabVs15 & GrnVs15>UKIPVs15 & GrnVs15>PCVs15 & GrnVs15>OtherVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==1) & (Second15==3) & (OtherVs15>LabVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15 & OtherVs15>SNPVs15))

replace Third15=3 if ((Winner15==1) & (Second15==4) & (LDVs15>UKIPVs15 & LDVs15>LabVs15 & LDVs15>GrnVs15 & LDVs15>PCVs15 & LDVs15>OtherVs15))
replace Third15=2 if ((Winner15==1) & (Second15==4) & (LabVs15>LDVs15 & LabVs15>UKIPVs15 & LabVs15>GrnVs15 & LabVs15>PCVs15 & LabVs15>OtherVs15))
replace Third15=5 if ((Winner15==1) & (Second15==4) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>LabVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==1) & (Second15==4) & (UKIPVs15>LDVs15 & UKIPVs15>LabVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15))
replace Third15=7 if ((Winner15==1) & (Second15==4) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>LabVs15 & GrnVs15>PCVs15 & GrnVs15>OtherVs15))
replace Third15=8 if ((Winner15==1) & (Second15==4) & (OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>LabVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15))

replace Third15=2 if ((Winner15==1) & (Second15==6) & (LabVs15>LDVs1 & LabVs15>GrnVs15 & LabVs15>OtherVs15 & LabVs15>SNPVs15 & LabVs15>PCVs15))
replace Third15=3 if ((Winner15==1) & (Second15==6) & (LDVs15>LabVs1 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15 & LDVs15>PCVs15))
replace Third15=4 if ((Winner15==1) & (Second15==6) & (SNPVs15>LabVs1 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15 & SNPVs15>LDVs15 & SNPVs15>PCVs15))
replace Third15=5 if ((Winner15==1) & (Second15==6) & (PCVs15>LabVs1 & PCVs15>GrnVs15 & PCVs15>OtherVs15 & PCVs15>LDVs15 & PCVs15>SNPVs15))
replace Third15=6 if ((Winner15==1) & (Second15==6) & (GrnVs15>LabVs1 & GrnVs15>PCVs15 & GrnVs15>OtherVs15 & GrnVs15>LDVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==1) & (Second15==6) & (OtherVs15>LabVs1 & OtherVs15>GrnVs15 & OtherVs15>PCVs15 & OtherVs15>LDVs15 & OtherVs15>SNPVs15))

replace Third15=3 if ((Winner15==1) & (Second15==8) & (LDVs15>UKIPVs15 & LDVs15>LabVs15 & LDVs15>PCVs15 & LDVs15>GrnVs15 & LDVs15>SNPVs15))
replace Third15=2 if ((Winner15==1) & (Second15==8) & (LabVs15>LDVs15 & LabVs15>UKIPVs15  & LabVs15>PCVs15 & LabVs15>GrnVs15& LabVs15>SNPVs15))
replace Third15=4 if ((Winner15==1) & (Second15==8) & (SNPVs15>LDVs15 & SNPVs15>UKIPVs15 & SNPVs15>LabVs15 & SNPVs15>GrnVs15))
replace Third15=5 if ((Winner15==1) & (Second15==8) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>LabVs15 & PCVs15>GrnVs15))
replace Third15=6 if ((Winner15==1) & (Second15==8) & (UKIPVs15>LDVs15 & UKIPVs15>LabVs15 & UKIPVs15>PCVs15 & UKIPVs15>GrnVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==1) & (Second15==8) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>LabVs15 & GrnVs15>PCVs15 & GrnVs15>SNPVs15))

* Lab as winner: 

replace Third15=3 if ((Winner15==2) & (Second15==1) & (LDVs15>UKIPVs15 & LDVs15>GrnVs15 & LDVs15>PCVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15))
replace Third15=4 if ((Winner15==2) & (Second15==1) & (SNPVs15>LDVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==2) & (Second15==1) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==2) & (Second15==1) & (UKIPVs15>LDVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==2) & (Second15==1) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15  & GrnVs15>PCVs15 & GrnVs15>OtherVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==2) & (Second15==1) & (OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15 & OtherVs15>SNPVs15))

replace Third15=1 if ((Winner15==2) & (Second15==3) & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>PCVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15))
replace Third15=4 if ((Winner15==2) & (Second15==3) & (SNPVs15>ConVs15 & SNPVs15>UKIPVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==2) & (Second15==3) & (PCVs15>ConVs15 & PCVs15>UKIPVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==2) & (Second15==3) & (UKIPVs15>ConVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==2) & (Second15==3) & (GrnVs15>ConVs15 & GrnVs15>UKIPVs15 & GrnVs15>PCVs15 & GrnVs15>OtherVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==2) & (Second15==3) & (OtherVs15>ConVs15 & OtherVs15>UKIPVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15 & OtherVs15>SNPVs15))

replace Third15=3 if ((Winner15==2) & (Second15==4) & (LDVs15>UKIPVs15 & LDVs15>ConVs15 & LDVs15>GrnVs15 & LDVs15>PCVs15 & LDVs15>OtherVs15))
replace Third15=1 if ((Winner15==2) & (Second15==4) & (ConVs15>LDVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>PCVs15 & ConVs15>OtherVs15))
replace Third15=5 if ((Winner15==2) & (Second15==4) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>ConVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=6 if ((Winner15==2) & (Second15==4) & (UKIPVs15>LDVs15 & UKIPVs15>ConVs15 & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15))
replace Third15=7 if ((Winner15==2) & (Second15==4) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>ConVs15 & GrnVs15>PCVs15 & GrnVs15>OtherVs15))
replace Third15=8 if ((Winner15==2) & (Second15==4) & (OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>ConVs15 & OtherVs15>GrnVs15 & OtherVs15>PCVs15))

replace Third15=3 if ((Winner15==2) & (Second15==5) & (LDVs15>UKIPVs15 & LDVs15>ConVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15))
replace Third15=1 if ((Winner15==2) & (Second15==5) & (ConVs15>LDVs15 & ConVs15>UKIPVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15))
replace Third15=6 if ((Winner15==2) & (Second15==5) & (UKIPVs15>LDVs15 & UKIPVs15>ConVs15 & UKIPVs15>GrnVs15 & UKIPVs15>OtherVs15))
replace Third15=7 if ((Winner15==2) & (Second15==5) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>ConVs15 & GrnVs15>OtherVs15))
replace Third15=8 if ((Winner15==2) & (Second15==5) & (OtherVs15>LDVs15 & OtherVs15>UKIPVs15 & OtherVs15>ConVs15 & OtherVs15>GrnVs15))

replace Third15=3 if ((Winner15==2) & (Second15==6) & (LDVs15>PCVs15 & LDVs15>ConVs15 & LDVs15>GrnVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15))
replace Third15=1 if ((Winner15==2) & (Second15==6) & (ConVs15>LDVs15 & ConVs15>PCVs15 & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15))
replace Third15=4 if ((Winner15==2) & (Second15==6) & (SNPVs15>LDVs15 & SNPVs15>ConVs15 & SNPVs15>GrnVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==2) & (Second15==6) & (PCVs15>LDVs15 & PCVs15>ConVs15 & PCVs15>GrnVs15 & PCVs15>OtherVs15))
replace Third15=7 if ((Winner15==2) & (Second15==6) & (GrnVs15>LDVs15 & GrnVs15>PCVs15 & GrnVs15>ConVs15 & GrnVs15>OtherVs15 & GrnVs15>SNPVs15))
replace Third15=8 if ((Winner15==2) & (Second15==6) & (OtherVs15>LDVs15 & OtherVs15>PCVs15 & OtherVs15>ConVs15 & OtherVs15>GrnVs15 & OtherVs15>SNPVs15))

replace Third15=3 if ((Winner15==2) & (Second15==7) & (LDVs15>PCVs15 & LDVs15>ConVs15 & LDVs15>UKIPVs15 & LDVs15>OtherVs15 & LDVs15>SNPVs15))
replace Third15=1 if ((Winner15==2) & (Second15==7) & (ConVs15>LDVs15 & ConVs15>PCVs15 & ConVs15>UKIPVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15))
replace Third15=4 if ((Winner15==2) & (Second15==7) & (SNPVs15>LDVs15 & SNPVs15>ConVs15 & SNPVs15>UKIPVs15 & SNPVs15>OtherVs15))
replace Third15=5 if ((Winner15==2) & (Second15==7) & (PCVs15>LDVs15 & PCVs15>ConVs15 & PCVs15>UKIPVs15 & PCVs15>OtherVs15))
replace Third15=7 if ((Winner15==2) & (Second15==7) & (UKIPVs15>LDVs15 & UKIPVs15>PCVs15 & UKIPVs15>ConVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15))
replace Third15=8 if ((Winner15==2) & (Second15==7) & (OtherVs15>LDVs15 & OtherVs15>PCVs15 & OtherVs15>ConVs15 & OtherVs15>UKIPVs15 & OtherVs15>SNPVs15))

replace Third15=3 if ((Winner15==2) & (Second15==8) & (LDVs15>UKIPVs15 & LDVs15>ConVs15 & LDVs15>PCVs15 & LDVs15>GrnVs15 & LDVs15>SNPVs15))
replace Third15=1 if ((Winner15==2) & (Second15==8) & (ConVs15>LDVs15 & ConVs15>UKIPVs15  & ConVs15>PCVs15 & ConVs15>GrnVs15& ConVs15>SNPVs15))
replace Third15=4 if ((Winner15==2) & (Second15==8) & (SNPVs15>LDVs15 & SNPVs15>UKIPVs15 & SNPVs15>ConVs15 & SNPVs15>GrnVs15))
replace Third15=5 if ((Winner15==2) & (Second15==8) & (PCVs15>LDVs15 & PCVs15>UKIPVs15 & PCVs15>ConVs15 & PCVs15>GrnVs15))
replace Third15=6 if ((Winner15==2) & (Second15==8) & (UKIPVs15>LDVs15 & UKIPVs15>ConVs15 & UKIPVs15>PCVs15 & UKIPVs15>GrnVs15 & UKIPVs15>SNPVs15))
replace Third15=7 if ((Winner15==2) & (Second15==8) & (GrnVs15>LDVs15 & GrnVs15>UKIPVs15 & GrnVs15>ConVs15 & GrnVs15>PCVs15 & GrnVs15>SNPVs15))

* LD as winner: 

replace Third15=2 if Winner15==3 & Second15==1 & (LabVs15>UKIPVs15  & LabVs15>GrnVs15 & LabVs15>PCVs15 & LabVs15>OtherVs15 & LabVs15>SNPVs15)
replace Third15=6 if Winner15==3 & Second15==1 & (UKIPVs15>LabVs15  & UKIPVs15>GrnVs15 & UKIPVs15>PCVs15 & UKIPVs15>OtherVs15 & UKIPVs15>SNPVs15)

replace Third15=1 if Winner15==3 & Second15==2 & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>PCVs15 & ConVs15>OtherVs15 & ConVs15>SNPVs15)

replace Third15=2 if Winner15==3 & Second15==4 & (LabVs15>UKIPVs15  & LabVs15>GrnVs15 & LabVs15>PCVs15 & LabVs15>OtherVs15 & LabVs15>ConVs15)
replace Third15=1 if Winner15==3 & Second15==4 & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>PCVs15 & ConVs15>OtherVs15 & ConVs15>LabVs15)

replace Third15=1 if Winner15==3 & Second15==5 & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>OtherVs15 & ConVs15>LabVs15)


* SNP as winner:
replace Third15=1 if Winner15==4 & Second15==2 & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>LDVs15 & ConVs15>OtherVs15)

replace Third15=2 if Winner15==4 & Second15==1 & (LabVs15>UKIPVs15  & LabVs15>GrnVs15 & LabVs15>LDVs15 & LabVs15>OtherVs15)
replace Third15=3 if Winner15==4 & Second15==1 & (LDVs15>UKIPVs15  & LDVs15>GrnVs15 & LDVs15>LabVs15 & LDVs15>OtherVs15)

replace Third15=1 if Winner15==4 & Second15==3 & (ConVs15>UKIPVs15  & ConVs15>GrnVs15 & ConVs15>LabVs15 & ConVs15>OtherVs15)
replace Third15=2 if Winner15==4 & Second15==3 & (LabVs15>UKIPVs15  & LabVs15>GrnVs15 & LabVs15>ConVs15 & LabVs15>OtherVs15)

* PC as winner 
tab pano if Winner15==5

tab PCVs15 if pano==16
tab LabVs15 if pano==16
tab ConVs15 if pano==16
tab LDVs15 if pano==16
tab UKIPVs15 if pano==16

replace Third15=1 if pano==16

tab PCVs15 if pano==217
tab LabVs15 if pano==217
tab ConVs15 if pano==217
tab LDVs15 if pano==217
tab UKIPVs15 if pano==217

replace Third15=2 if pano==217

*UKIP as winner
tab pano if Winner15==6

tab LabVs15 if pano==152
tab ConVs15 if pano==152
tab LDVs15 if pano==152
tab UKIPVs15 if pano==152

replace Third15=2 if pano==152

* Green as winner
tab pano if Winner15==7

tab LabVs15 if pano==98
tab ConVs15 if pano==98
tab LDVs15 if pano==98
tab UKIPVs15 if pano==98

replace Third15=1 if pano==98


tab Third15
tab Second15
tab Winner15

tab pano if Winner15!=. & Third15==.

tab Winner15 if pano==131
tab Second15 if pano==131

tab LabVs15 if pano==131
tab ConVs15 if pano==131
tab LDVs15 if pano==131
tab UKIPVs15 if pano==131
tab GrnVs15 if pano==131

replace Third15=1 if pano==131


* labelling values 

tab Third15
label values Third15 con_label
tab Third15

*** Fourth party indicator:

* coding an empty indicator 

gen Fourth15=.

* coding this manually, due to the small number of constituencies with 4 effective parties:

replace Fourth15=6 if Winner15==1 & Second15==2 & Third15==3 & (UKIPVs15>PCVs15 & UKIPVs15>SNPVs15 & UKIPVs15>OtherVs15) & eff_parties15==4
replace Fourth15=6 if Winner15==2 & Second15==1 & Third15==3 & (UKIPVs15>PCVs15 & UKIPVs15>SNPVs15 & UKIPVs15>OtherVs15) & eff_parties15==4
replace Fourth15=3 if Winner15==1 & Second15==2 & Third15==6 & (LDVs15>PCVs15 & LDVs15>SNPVs15 & LDVs15>OtherVs15) & eff_parties15==4
replace Fourth15=3 if Winner15==2 & Second15==1 & Third15==6 & (LDVs15>PCVs15 & LDVs15>SNPVs15 & LDVs15>OtherVs15) & eff_parties15==4

tab Winner15 if pano==2

tab PCVs15 if pano==2
tab SNPVs15 if pano==2
tab OtherVs15 if pano==2
tab LabVs15 if pano==2
tab ConVs15 if pano==2
tab LDVs15 if pano==2
tab UKIPVs15 if pano==2

replace Fourth15=6 if pano==2

tab PCVs15 if pano==36
tab SNPVs15 if pano==36
tab OtherVs15 if pano==36
tab LabVs15 if pano==36
tab ConVs15 if pano==36
tab LDVs15 if pano==36
tab UKIPVs15 if pano==36

replace Fourth15=6 if pano==36

tab PCVs15 if pano==65
tab SNPVs15 if pano==65
tab OtherVs15 if pano==65
tab LabVs15 if pano==65
tab ConVs15 if pano==65
tab LDVs15 if pano==65
tab UKIPVs15 if pano==65

replace Fourth15=1 if pano==65

tab PCVs15 if pano==89
tab SNPVs15 if pano==89
tab OtherVs15 if pano==89
tab LabVs15 if pano==89
tab ConVs15 if pano==89
tab LDVs15 if pano==89
tab UKIPVs15 if pano==89

replace Fourth15=6 if pano==89

tab PCVs15 if pano==94
tab SNPVs15 if pano==94
tab OtherVs15 if pano==94
tab LabVs15 if pano==94
tab ConVs15 if pano==94
tab LDVs15 if pano==94
tab UKIPVs15 if pano==94

replace Fourth15=5 if pano==94

tab PCVs15 if pano==102
tab SNPVs15 if pano==102
tab OtherVs15 if pano==102
tab LabVs15 if pano==102
tab ConVs15 if pano==102
tab LDVs15 if pano==102
tab UKIPVs15 if pano==102

replace Fourth15=8 if pano==102

tab PCVs15 if pano==109
tab SNPVs15 if pano==109
tab OtherVs15 if pano==109
tab LabVs15 if pano==109
tab ConVs15 if pano==109
tab LDVs15 if pano==109
tab UKIPVs15 if pano==109

replace Fourth15=1 if pano==109

tab PCVs15 if pano==114
tab SNPVs15 if pano==114
tab OtherVs15 if pano==114
tab LabVs15 if pano==114
tab ConVs15 if pano==114
tab LDVs15 if pano==114
tab UKIPVs15 if pano==114

replace Fourth15=5 if pano==114

tab PCVs15 if pano==119
tab SNPVs15 if pano==119
tab OtherVs15 if pano==119
tab LabVs15 if pano==119
tab ConVs15 if pano==119
tab LDVs15 if pano==119
tab UKIPVs15 if pano==119

replace Fourth15=6 if pano==119

tab PCVs15 if pano==126
tab SNPVs15 if pano==126
tab OtherVs15 if pano==126
tab LabVs15 if pano==126
tab ConVs15 if pano==126
tab LDVs15 if pano==126
tab UKIPVs15 if pano==126

replace Fourth15=6 if pano==126

tab PCVs15 if pano==128
tab SNPVs15 if pano==128
tab OtherVs15 if pano==128
tab LabVs15 if pano==128
tab ConVs15 if pano==128
tab LDVs15 if pano==128
tab UKIPVs15 if pano==128

replace Fourth15=5 if pano==128

tab PCVs15 if pano==129
tab SNPVs15 if pano==129
tab OtherVs15 if pano==129
tab LabVs15 if pano==129
tab ConVs15 if pano==129
tab LDVs15 if pano==129
tab UKIPVs15 if pano==129

replace Fourth15=6 if pano==129

tab PCVs15 if pano==131
tab SNPVs15 if pano==131
tab OtherVs15 if pano==131
tab LabVs15 if pano==131
tab ConVs15 if pano==131
tab LDVs15 if pano==131
tab UKIPVs15 if pano==131

replace Fourth15=6 if pano==131

tab PCVs15 if pano==133
tab SNPVs15 if pano==133
tab OtherVs15 if pano==133
tab LabVs15 if pano==133
tab ConVs15 if pano==133
tab LDVs15 if pano==133
tab UKIPVs15 if pano==133

replace Fourth15=6 if pano==133

tab PCVs15 if pano==135
tab SNPVs15 if pano==135
tab OtherVs15 if pano==135
tab LabVs15 if pano==135
tab ConVs15 if pano==135
tab LDVs15 if pano==135
tab UKIPVs15 if pano==135

replace Fourth15=6 if pano==135

tab PCVs15 if pano==135
tab SNPVs15 if pano==135
tab OtherVs15 if pano==135
tab LabVs15 if pano==135
tab ConVs15 if pano==135
tab LDVs15 if pano==135
tab UKIPVs15 if pano==135

replace Fourth15=6 if pano==135

tab PCVs15 if pano==154
tab SNPVs15 if pano==154
tab OtherVs15 if pano==154
tab LabVs15 if pano==154
tab ConVs15 if pano==154
tab LDVs15 if pano==154
tab UKIPVs15 if pano==154

replace Fourth15=5 if pano==154

tab PCVs15 if pano==157
tab SNPVs15 if pano==157
tab OtherVs15 if pano==157
tab LabVs15 if pano==157
tab ConVs15 if pano==157
tab LDVs15 if pano==157
tab UKIPVs15 if pano==157

replace Fourth15=6 if pano==157

tab PCVs15 if pano==217
tab SNPVs15 if pano==217
tab OtherVs15 if pano==217
tab LabVs15 if pano==217
tab ConVs15 if pano==217
tab LDVs15 if pano==217
tab UKIPVs15 if pano==217

replace Fourth15=6 if pano==217

tab PCVs15 if pano==296
tab SNPVs15 if pano==296
tab OtherVs15 if pano==296
tab LabVs15 if pano==296
tab ConVs15 if pano==296
tab LDVs15 if pano==296
tab UKIPVs15 if pano==296

replace Fourth15=8 if pano==296

tab PCVs15 if pano==333
tab SNPVs15 if pano==333
tab OtherVs15 if pano==333
tab LabVs15 if pano==333
tab ConVs15 if pano==333
tab LDVs15 if pano==333
tab UKIPVs15 if pano==333

replace Fourth15=3 if pano==333

tab PCVs15 if pano==354
tab SNPVs15 if pano==354
tab OtherVs15 if pano==354
tab LabVs15 if pano==354
tab ConVs15 if pano==354
tab LDVs15 if pano==354
tab UKIPVs15 if pano==354

replace Fourth15=6 if pano==354

tab PCVs15 if pano==375
tab SNPVs15 if pano==375
tab OtherVs15 if pano==375
tab LabVs15 if pano==375
tab ConVs15 if pano==375
tab LDVs15 if pano==375
tab UKIPVs15 if pano==375

replace Fourth15=1 if pano==375

tab PCVs15 if pano==408
tab SNPVs15 if pano==408
tab OtherVs15 if pano==408
tab LabVs15 if pano==408
tab ConVs15 if pano==408
tab LDVs15 if pano==408
tab UKIPVs15 if pano==408

replace Fourth15=1 if pano==408

tab PCVs15 if pano==431
tab SNPVs15 if pano==431
tab OtherVs15 if pano==431
tab LabVs15 if pano==431
tab ConVs15 if pano==431
tab LDVs15 if pano==431
tab UKIPVs15 if pano==431

replace Fourth15=6 if pano==431

tab PCVs15 if pano==458
tab SNPVs15 if pano==458
tab OtherVs15 if pano==458
tab LabVs15 if pano==458
tab ConVs15 if pano==458
tab LDVs15 if pano==458
tab UKIPVs15 if pano==458

replace Fourth15=6 if pano==458

tab PCVs15 if pano==459
tab SNPVs15 if pano==459
tab OtherVs15 if pano==459
tab LabVs15 if pano==459
tab ConVs15 if pano==459
tab LDVs15 if pano==459
tab UKIPVs15 if pano==459

replace Fourth15=8 if pano==459

tab PCVs15 if pano==491
tab SNPVs15 if pano==491
tab OtherVs15 if pano==491
tab LabVs15 if pano==491
tab ConVs15 if pano==491
tab LDVs15 if pano==491
tab UKIPVs15 if pano==491

replace Fourth15=2 if pano==491

tab PCVs15 if pano==494
tab SNPVs15 if pano==494
tab OtherVs15 if pano==494
tab LabVs15 if pano==494
tab ConVs15 if pano==494
tab LDVs15 if pano==494
tab UKIPVs15 if pano==494

replace Fourth15=2 if pano==494

tab PCVs15 if pano==526
tab SNPVs15 if pano==526
tab OtherVs15 if pano==526
tab LabVs15 if pano==526
tab ConVs15 if pano==526
tab LDVs15 if pano==526
tab UKIPVs15 if pano==526

replace Fourth15=6 if pano==526

tab PCVs15 if pano==537
tab SNPVs15 if pano==537
tab OtherVs15 if pano==537
tab LabVs15 if pano==537
tab ConVs15 if pano==537
tab LDVs15 if pano==537
tab UKIPVs15 if pano==537

replace Fourth15=8 if pano==537

tab PCVs15 if pano==579
tab SNPVs15 if pano==579
tab OtherVs15 if pano==579
tab LabVs15 if pano==579
tab ConVs15 if pano==579
tab LDVs15 if pano==579
tab UKIPVs15 if pano==579

replace Fourth15=6 if pano==579

tab PCVs15 if pano==641
tab SNPVs15 if pano==641
tab OtherVs15 if pano==641
tab LabVs15 if pano==641
tab ConVs15 if pano==641
tab LDVs15 if pano==641
tab UKIPVs15 if pano==641

replace Fourth15=5 if pano==641

tab PCVs15 if pano==647
tab SNPVs15 if pano==647
tab OtherVs15 if pano==647
tab LabVs15 if pano==647
tab ConVs15 if pano==647
tab LDVs15 if pano==647
tab UKIPVs15 if pano==647

replace Fourth15=6 if pano==647

* labelling this variable 

tab Fourth15
label values Fourth15 con_label
tab Fourth15
tab eff_parties15

*** Next, we code effective party identifier and effective party vote share variables:

tab eff_party_15_1
tab Winner15

** For the first effective party:
* identifier var

gen efp1_id_15=.
replace efp1_id_15=1 if eff_party_15_1==0 & Winner15==1
replace efp1_id_15=2 if eff_party_15_1==0 & Winner15==2
replace efp1_id_15=3 if eff_party_15_1==0 & Winner15==3
replace efp1_id_15=4 if eff_party_15_1==0 & Winner15==4
replace efp1_id_15=5 if eff_party_15_1==0 & Winner15==5
replace efp1_id_15=6 if eff_party_15_1==0 & Winner15==6
replace efp1_id_15=7 if eff_party_15_1==0 & Winner15==7

label values efp1_id_15 con_label
tab efp1_id_15

* vote share var:

gen efp1_VS_15=.
replace efp1_VS_15=ConVs15 if eff_party_15_1==0 & Winner15==1
replace efp1_VS_15=LabVs15 if eff_party_15_1==0 & Winner15==2
replace efp1_VS_15=LDVs15 if eff_party_15_1==0 & Winner15==3
replace efp1_VS_15=SNPVs15 if eff_party_15_1==0 & Winner15==4
replace efp1_VS_15=PCVs15 if eff_party_15_1==0 & Winner15==5
replace efp1_VS_15=UKIPVs15 if eff_party_15_1==0 & Winner15==6
replace efp1_VS_15=GrnVs15 if eff_party_15_1==0 & Winner15==7
tab efp1_VS_15

** For the second effective party:
* identifier var

tab eff_party_15_2
tab Second15

gen efp2_id_15=.
replace efp2_id_15=1 if eff_party_15_2==0 & Second15==1
replace efp2_id_15=2 if eff_party_15_2==0 & Second15==2
replace efp2_id_15=3 if eff_party_15_2==0 & Second15==3
replace efp2_id_15=4 if eff_party_15_2==0 & Second15==4
replace efp2_id_15=5 if eff_party_15_2==0 & Second15==5
replace efp2_id_15=6 if eff_party_15_2==0 & Second15==6
replace efp2_id_15=7 if eff_party_15_2==0 & Second15==7
replace efp2_id_15=8 if eff_party_15_2==0 & Second15==8

label values efp2_id_15 con_label
tab efp2_id_15

* vote share var

gen efp2_VS_15=.
replace efp2_VS_15=ConVs15 if eff_party_15_2==0 & Second15==1
replace efp2_VS_15=LabVs15 if eff_party_15_2==0 & Second15==2
replace efp2_VS_15=LDVs15 if eff_party_15_2==0 & Second15==3
replace efp2_VS_15=SNPVs15 if eff_party_15_2==0 & Second15==4
replace efp2_VS_15=PCVs15 if eff_party_15_2==0 & Second15==5
replace efp2_VS_15=UKIPVs15 if eff_party_15_2==0 & Second15==6
replace efp2_VS_15=GrnVs15 if eff_party_15_2==0 & Second15==7
replace efp2_VS_15=OtherVs15 if eff_party_15_2==0 & Second15==8
tab efp2_VS_15

** For the third effective party:
* identifier var

tab eff_party_15_3
tab Third15

gen efp3_id_15=.
replace efp3_id_15=1 if eff_party_15_3==0 & Third15==1
replace efp3_id_15=2 if eff_party_15_3==0 & Third15==2
replace efp3_id_15=3 if eff_party_15_3==0 & Third15==3
replace efp3_id_15=5 if eff_party_15_3==0 & Third15==5
replace efp3_id_15=6 if eff_party_15_3==0 & Third15==6
replace efp3_id_15=7 if eff_party_15_3==0 & Third15==7
replace efp3_id_15=8 if eff_party_15_3==0 & Third15==8

label values efp3_id_15 con_label
tab efp3_id_15

* vote share var

gen efp3_VS_15=.
replace efp3_VS_15=ConVs15 if eff_party_15_3==0 & Third15==1
replace efp3_VS_15=LabVs15 if eff_party_15_3==0 & Third15==2
replace efp3_VS_15=LDVs15 if eff_party_15_3==0 & Third15==3
replace efp3_VS_15=PCVs15 if eff_party_15_3==0 & Third15==5
replace efp3_VS_15=UKIPVs15 if eff_party_15_3==0 & Third15==6
replace efp3_VS_15=GrnVs15 if eff_party_15_3==0 & Third15==7
replace efp3_VS_15=OtherVs15 if eff_party_15_3==0 & Third15==8
tab efp3_VS_15

** For the fourth effective party:
* identifier var

tab eff_party_15_4
tab Fourth15

gen efp4_id_15=.
replace efp4_id_15=1 if eff_party_15_4==0 & Fourth15==1
replace efp4_id_15=2 if eff_party_15_4==0 & Fourth15==2
replace efp4_id_15=3 if eff_party_15_4==0 & Fourth15==3
replace efp4_id_15=5 if eff_party_15_4==0 & Fourth15==5
replace efp4_id_15=6 if eff_party_15_4==0 & Fourth15==6
replace efp4_id_15=8 if eff_party_15_4==0 & Fourth15==8
label values efp4_id_15 con_label
tab efp4_id_15

* vote share var 

gen efp4_VS_15=.
replace efp4_VS_15=ConVs15 if eff_party_15_4==0 & Fourth15==1
replace efp4_VS_15=LabVs15 if eff_party_15_4==0 & Fourth15==2
replace efp4_VS_15=LDVs15 if eff_party_15_4==0 & Fourth15==3
replace efp4_VS_15=PCVs15 if eff_party_15_4==0 & Fourth15==5
replace efp4_VS_15=UKIPVs15 if eff_party_15_4==0 & Fourth15==6
replace efp4_VS_15=OtherVs15 if eff_party_15_4==0 & Fourth15==8
tab efp4_VS_15

* Number of effective parties in each race: 

tab eff_parties15

*Total frequency at which a party was effective:

numlabel, add

tab efp1_id_15
tab efp2_id_15
tab efp3_id_15
tab efp4_id_15


* Calculating competiveness index: 

gen CI1_15 = efp1_VS_15 if eff_parties15==1
gen CI2_15 = ((efp1_VS_15*efp2_VS_15)*(eff_parties15^eff_parties15)) if eff_parties15==2
gen CI3_15 = ((efp1_VS_15*efp2_VS_15*efp3_VS_15)*(eff_parties15^eff_parties15)) if eff_parties15==3
gen CI4_15 = ((efp1_VS_15*efp2_VS_15*efp3_VS_15*efp4_VS_15)*(eff_parties15^eff_parties15)) if eff_parties15==4

egen CI_15 = rowmax(CI1_15 CI2_15 CI3_15 CI4_15)

tab CI_15
summ CI_15

* Calculating raw margin variable 

gen raw_margin=(Majority15/100)*TotalVote15

gen raw_margin_W6=round(raw_margin)

* Dummy for whether Brexit Party or UKIP was an effective party 

bysort pano: gen farright_ep_W6=0
bysort pano: replace farright_ep_W6=1 if efp1_id_15==6|efp2_id_15==6|efp3_id_15==6|efp4_id_15==6

tab farright_ep_W6


keep CI_15 Winner15 Second15 Third15 Fourth15 ConVs15 LabVs15 LDVs15 SNPVs15 PCVs15 UKIPVs15 GrnVs15 pano ConstituencyName ONSConstID eff_parties15 efp1_id_15 efp2_id_15 efp3_id_15 efp4_id_15 raw_margin_W6 farright_ep_W6 Electorate15

rename pano panoW6

save CI_2015_v2.dta, replace

use CI_2015_v2.dta, clear

summ CI_15

**# 4. Coding campaign spend for 2015

* loading in the data 

clear 

import excel using "2015 Candidate Campaign Spending.xlsx", firstrow

* drop NI candidates 

tab Nation
drop if Nation==4

** retaining only major parties 

* coding numeric party var

encode PartyName, gen(party_number)

tab party_number

numlabel party_number, add

* Coding a candidate party identifier var that (a) is limited to the parties used for the aff pol variable and (b) follows the same numeric dcoding structure as the aff pol and CI variables

browse

*EFP ID - Con=1; Lab=2; LD=3; SNP=4; PC = 5; UKIP = 6; Green = 7; Other = 8; Brexit= 9.

gen candidate_PID=1 if party_number==25
replace candidate_PID=2 if party_number==44
replace candidate_PID=3 if party_number==49
replace candidate_PID=4 if party_number==79
replace candidate_PID=5 if party_number==68
replace candidate_PID=6 if party_number==116
replace candidate_PID=7 if party_number==32
replace candidate_PID=9999 if party_number!=25&party_number!=44&party_number!=49&party_number!=79&party_number!=68&party_number!=116&party_number!=32

tab candidate_PID

* calculating dummy indicator for parties of interest 

gen major_p_candidate=1 if candidate_PID<=7
replace major_p_candidate=0 if candidate_PID>7

tab major_p_candidate

* Calculating total n variable for major candidates per constituency, needed to code the average measure.

bysort ConstituencyName: egen major_candidate_N=total(major_p_candidate) 

tab major_candidate_N

keep if major_p_candidate==1

** Three approaches - major party mean spend as proportion of limit, major party total spend, and in-party and out-party spending 

** Major party mean spend 
* first, calculate candidate spend as proportion of limit

summ ShortTotalSpend
summ ShortLimit

bysort CandId: gen R_cand_spend_2015=ShortTotalSpend/ShortLimit
summ R_cand_spend_2015

* second, calculate absolute candidate spending 

rename ShortTotalSpend A_cand_spend_2015

* Calculating constituency average relative spend per major candidate in each constituency 

bysort ConstituencyName: egen R_con_spend_total=total(R_cand_spend_2015)

gen R_con_spend_2015=R_con_spend_total/major_candidate_N

* Calculating absolute major candidate spend in each constituency 

bysort ConstituencyName: egen A_con_spend_2015=total(A_cand_spend_2015)

* adjust the absolute spending score for inflation 

gen A_con_spend_W6 = 1.07747748*A_con_spend_2015

* generating party-specific spending:

bysort ConstituencyName: gen Conservative_spend=A_cand_spend_2015 if candidate_PID==1
bysort ConstituencyName: gen Labour_spend=A_cand_spend_2015 if candidate_PID==2
bysort ConstituencyName: gen LD_spend=A_cand_spend_2015 if candidate_PID==3
bysort ConstituencyName: gen SNP_spend=A_cand_spend_2015 if candidate_PID==4
bysort ConstituencyName: gen PC_spend=A_cand_spend_2015 if candidate_PID==5
bysort ConstituencyName: gen UKIP_spend=A_cand_spend_2015 if candidate_PID==6
bysort ConstituencyName: gen Green_spend=A_cand_spend_2015 if candidate_PID==7

* apply this to all obs per constituency, in order to create a constituency-level dataset

bysort ConstituencyName: egen Conservative_spend_2015=max(Conservative_spend)
bysort ConstituencyName: egen Labour_spend_2015=max(Labour_spend)
bysort ConstituencyName: egen LD_spend_2015=max(LD_spend)
bysort ConstituencyName: egen SNP_spend_2015=max(SNP_spend)
bysort ConstituencyName: egen PC_spend_2015=max(PC_spend)
bysort ConstituencyName: egen UKIP_spend_2015=max(UKIP_spend)
bysort ConstituencyName: egen Green_spend_2015=max(Green_spend)

* Adjust these party-specific spending scores for inflation

gen Conservative_spend_W6 = 1.07747748*Conservative_spend_2015
gen Labour_spend_W6 = 1.07747748*Labour_spend_2015
gen LD_spend_W6 = 1.07747748*LD_spend_2015
gen SNP_spend_W6 = 1.07747748*SNP_spend_2015
gen PC_spend_W6 = 1.07747748*PC_spend_2015
gen UKIP_spend_W6 = 1.07747748*UKIP_spend_2015
gen Green_spend_W6 = 1.07747748*Green_spend_2015

* retaining only necessary vars

keep ConstituencyName R_con_spend_2015 A_con_spend_W6 Conservative_spend_W6 Labour_spend_W6 LD_spend_W6 SNP_spend_W6 PC_spend_W6 UKIP_spend_W6 Green_spend_W6

tab ConstituencyName if R_con_spend_2015==0
tab ConstituencyName if A_con_spend_W6==0

drop if R_con_spend_2015==0

* dropping duplicates so that there is only one observation per constituency

duplicates drop ConstituencyName, force

* renaming and destringing Constituency id var

rename ConstituencyName Constituency

* calculating mean scores for spending vars

summ R_con_spend_2015
summ A_con_spend_W6

* relative major party spend (per candidate): M = 0.3031. SD = 0.1286, N = 615
* Absolute major party spend: M = 21090.95; SD = 8958, N = 615

browse

save campaign_spending_2015.dta, replace

use campaign_spending_2015.dta, clear

**# 5. Coding campaign spend for 2017

* loading in the data

clear

import excel using "2017 Candidate Campaign Spending.xlsx", firstrow

* Dropping NI candidates 

tab Nation, missing
drop if Nation==4
drop if Nation==.

** Retaining only major parties

* coding numeric party var

tab Candidatesparty

encode Candidatesparty, gen(Candidatesparty_N)

numlabel Candidatesparty_N, add

tab Candidatesparty_N

* Coding a candidate party identifier var that (a) is limited to the parties used for the aff pol variable and (b) follows the same numeric dcoding structure as the aff pol and CI variables

*EFP ID - Con=1; Lab=2; LD=3; SNP=4; PC = 5; UKIP = 6; Green = 7; Other = 8; Brexit= 9.

gen candidate_PID=1 if Candidatesparty_N==16
replace candidate_PID=2 if Candidatesparty_N==26
replace candidate_PID=3 if Candidatesparty_N==28
replace candidate_PID=4 if Candidatesparty_N==41
replace candidate_PID=5 if Candidatesparty_N==38
replace candidate_PID=6 if Candidatesparty_N==60
replace candidate_PID=7 if Candidatesparty_N==21
replace candidate_PID=9999 if Candidatesparty_N!=16&Candidatesparty_N!=26&Candidatesparty_N!=28&Candidatesparty_N!=41&Candidatesparty_N!=38&Candidatesparty_N!=60&Candidatesparty_N!=21

tab candidate_PID

*Coding major party identifier 

gen major_p_candidate=1 if candidate_PID<=7
replace major_p_candidate=0 if candidate_PID>7

tab major_p_candidate

* Calculating total number of major candidates per constituency 

bysort Constituency: egen major_candidate_N=total(major_p_candidate) 

tab major_candidate_N

keep if major_p_candidate==1

** Major party mean spend and total spend 
* first, calculate candidate spend as proportion of limit

* there is already a variable measuring this, but in string format. 
* we thus first destring this variable:

browse 
tab SpendingasofLimit
destring SpendingasofLimit, gen(R_cand_spend_2017) ignore("A" "X" "x")

summ R_cand_spend_2017

* second, calculate absolute candidate spending 

tab TotalReportedSpending

destring TotalReportedSpending, gen(A_cand_spend_2017) ignore("A" "X" "x")

summ A_cand_spend_2017


* Calculating average relative spend per major candidate in each constituency 

bysort Constituency: egen R_con_spend_total=total(R_cand_spend_2017)

gen R_con_spend_2017=R_con_spend_total/major_candidate_N

summ R_con_spend_2017

* Calculating absolute major candidate spend in each constituency 

bysort Constituency:egen A_con_spend_2017=total(A_cand_spend_2017)

* adjust the absolute spending score for inflation 

gen A_con_spend_W13 = 1.04090513*A_con_spend_2017

* generating party-specific spending:

bysort Constituency: gen Conservative_spend=A_cand_spend_2017 if candidate_PID==1
bysort Constituency: gen Labour_spend=A_cand_spend_2017 if candidate_PID==2
bysort Constituency: gen LD_spend=A_cand_spend_2017 if candidate_PID==3
bysort Constituency: gen SNP_spend=A_cand_spend_2017 if candidate_PID==4
bysort Constituency: gen PC_spend=A_cand_spend_2017 if candidate_PID==5
bysort Constituency: gen UKIP_spend=A_cand_spend_2017 if candidate_PID==6
bysort Constituency: gen Green_spend=A_cand_spend_2017 if candidate_PID==7

* apply this to all obs per constituency, in order to create a constituency-level dataset

bysort Constituency: egen Conservative_spend_2017=max(Conservative_spend)
bysort Constituency: egen Labour_spend_2017=max(Labour_spend)
bysort Constituency: egen LD_spend_2017=max(LD_spend)
bysort Constituency: egen SNP_spend_2017=max(SNP_spend)
bysort Constituency: egen PC_spend_2017=max(PC_spend)
bysort Constituency: egen UKIP_spend_2017=max(UKIP_spend)
bysort Constituency: egen Green_spend_2017=max(Green_spend)

* Adjust these party-specific spending scores for inflation

gen Conservative_spend_W13 = 1.04090513*Conservative_spend_2017
gen Labour_spend_W13 = 1.04090513*Labour_spend_2017
gen LD_spend_W13 = 1.04090513*LD_spend_2017
gen SNP_spend_W13 = 1.04090513*SNP_spend_2017
gen PC_spend_W13 = 1.04090513*PC_spend_2017
gen UKIP_spend_W13 = 1.04090513*UKIP_spend_2017
gen Green_spend_W13 = 1.04090513*Green_spend_2017

*Drop all non-spending variables

keep Constituency R_con_spend_2017 A_con_spend_W13 Conservative_spend_W13 Labour_spend_W13 LD_spend_W13 SNP_spend_W13 PC_spend_W13 UKIP_spend_W13 Green_spend_W13

* dropping duplicates so that there is only one observation per constituency

duplicates drop Constituency, force

browse

* calculating mean scores for spending vars

summ R_con_spend_2017
summ A_con_spend_W13

* relative major party spend (per candidate): M = 0.33. SD = 0.15, N = 632
* Absolute major party spend: M = 20236, SD = 7754.92, N = 632

replace Constituency = "Ynys Mon" in 630

save campaign_spending_2017.dta, replace

use campaign_spending_2017.dta, clear

**# 6. Coding campaign spend for 2019

clear

import excel using "2019 Candidate Campaign Spending.xlsx", firstrow

keep Nation Constituency Candidatesparty TotalReportedSpending SpendingasofLimit* Forename Surname 

* dropping NI candidates 

tab Nation
drop if Nation=="Northern Ireland"

* Retaining only major parties 

* coding numeric party var

tab Candidatesparty

encode Candidatesparty, gen(Candidatesparty_N)

numlabel Candidatesparty_N, add

tab Candidatesparty_N

* Coding a candidate party identifier var that (a) is limited to the parties used for the aff pol variable and (b) follows the same numeric dcoding structure as the aff pol and CI variables

*EFP ID - Con=1; Lab=2; LD=3; SNP=4; PC = 5; Green = 7; Other = 8; Brexit= 9.

gen candidate_PID=1 if Candidatesparty_N==14
replace candidate_PID=2 if Candidatesparty_N==23
replace candidate_PID=3 if Candidatesparty_N==26
replace candidate_PID=4 if Candidatesparty_N==39
replace candidate_PID=5 if Candidatesparty_N==33
replace candidate_PID=7 if Candidatesparty_N==17
replace candidate_PID=9 if Candidatesparty_N==6
replace candidate_PID=9999 if Candidatesparty_N!=14&Candidatesparty_N!=23&Candidatesparty_N!=26&Candidatesparty_N!=39&Candidatesparty_N!=33&Candidatesparty_N!=17&Candidatesparty_N!=6

*Coding major party spend

tab candidate_PID
 
gen major_p_candidate=1 if candidate_PID<=9
replace major_p_candidate=0 if candidate_PID>9

tab major_p_candidate

bysort Constituency: egen major_candidate_N=total(major_p_candidate) 

tab major_candidate_N

keep if major_p_candidate==1

** Major party mean spend and total spend 
* first, calculate candidate spend as proportion of limit

* there are two variables in the data here - one for the spend info provided by candidates, and one for the spending limit stated on their return. We have used the spend provided by the candidates, but for those candidates who did not provide info, we have instead used the info from the return variable.

tab SpendingasofLimitprovided
destring SpendingasofLimitprovided, gen(R_cand_spend_2019) ignore("A" "C" "X" "n/a")

tab SpendingasofLimitstatedon
destring SpendingasofLimitstatedon, gen(R_cand_spend_B) ignore("A" "X" "n/a")

summ R_cand_spend_2019

replace R_cand_spend_2019=R_cand_spend_B if SpendingasofLimitprovided=="C"
replace R_cand_spend_2019=R_cand_spend_B if Constituency=="Charnwood"

* second, calculate absolute candidate spending 

tab TotalReportedSpending

destring TotalReportedSpending, gen(A_cand_spend_2019) ignore("A" "X" "n/a")

summ A_cand_spend_2019

* Calculating average relative spend per major candidate in each constituency 

bysort Constituency: egen R_con_spend_total=total(R_cand_spend_2019)

gen R_con_spend_2019=R_con_spend_total/major_candidate_N

summ R_con_spend_2019

* Calculating absolute major candidate spend in each constituency 

bysort Constituency:egen A_con_spend_W19=total(A_cand_spend_2019) 

* no inflation adjustments at this wave, as it is the base wave. 

* generating party-specific spending:

bysort Constituency: gen Conservative_spend=A_cand_spend_2019 if candidate_PID==1
bysort Constituency: gen Labour_spend=A_cand_spend_2019 if candidate_PID==2
bysort Constituency: gen LD_spend=A_cand_spend_2019 if candidate_PID==3
bysort Constituency: gen SNP_spend=A_cand_spend_2019 if candidate_PID==4
bysort Constituency: gen PC_spend=A_cand_spend_2019 if candidate_PID==5
bysort Constituency: gen UKIP_spend=A_cand_spend_2019 if candidate_PID==9
bysort Constituency: gen Green_spend=A_cand_spend_2019 if candidate_PID==7

* apply this to all obs per constituency, in order to create a constituency-level dataset

bysort Constituency: egen Conservative_spend_W19=max(Conservative_spend)
bysort Constituency: egen Labour_spend_W19=max(Labour_spend)
bysort Constituency: egen LD_spend_W19=max(LD_spend)
bysort Constituency: egen SNP_spend_W19=max(SNP_spend)
bysort Constituency: egen PC_spend_W19=max(PC_spend)
bysort Constituency: egen UKIP_spend_W19=max(UKIP_spend)
bysort Constituency: egen Green_spend_W19=max(Green_spend)

*Drop all non-spending variables

keep Constituency R_con_spend_2019 A_con_spend_W19 Conservative_spend_W19 Labour_spend_W19 LD_spend_W19 SNP_spend_W19 PC_spend_W19 UKIP_spend_W19 Green_spend_W19

* dropping duplicates so that there is only one observation per constituency

duplicates drop Constituency, force

* calculating mean scores for spending vars

tab Constituency if R_con_spend_2019==0
tab Constituency if A_con_spend_W19==0

recode R_con_spend_2019 A_con_spend_W19 (0=.)

summ R_con_spend_2019
summ A_con_spend_W19

* relative major party spend (per candidate): M = 0.4 SD = 0. 16, N = 626
* Absolute major party spend: M = 23621.66, SD = 8197.1. N = 626

save campaign_spending_2019.dta, replace

use campaign_spending_2019.dta, clear

summ Labour_spend_W19

**# ----- BESIP DATA CODING --------

* load in the individual-level BESIP data 

use BES2024_W29_Panel_v29.1.dta, clear

**# 1. 2015 - BESIP data coding

**# 1a. Merge with competitiveness data 

tab panoW6

* merge the individual-level data with the competitiveness data 

merge m:1 panoW6 using CI_2015_v2.dta

browse 


drop if CI_15==.

save bes_2015_temp.dta, replace

**# 1b. Coding individual-level variables 

use bes_2015_temp.dta, clear

*** PID as a moderator

* Binary measure

tab partyIdW3

recode partyIdW3 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_W6)

tab pid_pre_W6

* With the squeeze follow-up

gen pid_wsqueeze_w3=partyIdW3
replace pid_wsqueeze_w3=partyIdSqueezeW3 if partyIdW3==10|partyIdW3==9999
tab pid_wsqueeze_w3

recode pid_wsqueeze_w3 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_wsqueeze_W6)
tab pid_pre_wsqueeze_W6


* Categorical measure

tab partyIdStrengthW3

recode partyIdStrengthW3 (1=4 "Very strong") (2=3 "Fairly Strong") (3=2 "Not very strong") (9999=1 "No PID/Don't know"), gen(pid_strength_W6)
replace pid_strength_W6=1 if pid_pre_W6==0

tab pid_strength_W6

*** Controls 

** Education

recode p_edlevelW6 (4/5=1 "Degree") (0/3=0 "No degree"), gen (higheducW6)

** Age

summ ageW6

** Ethnicity 

recode p_ethnicityW6 (1=1 "White British") (else=0 "Other"), gen (ethnicW6)

** country 

tab countryW6

** Gender 

gen genderW6=.
replace genderW6=0 if gender==1 & wave6==1
replace genderW6 = 1 if gender==2 & wave6==1

** National election outcome

tab generalElectionVoteW6
recode generalElectionVoteW6 (1=1) (2/9=0) (9999=.), gen(wingovW6)
tab wingovW6

label define win_label 0 "Loss" 1 "Win"
label values wingovW6 win_label

** Local outcome

gen winconW6=0
replace winconW6=1 if ((generalElectionVoteW6==1 & Winner15==1)|(generalElectionVoteW6==2 & Winner15==2)|(generalElectionVoteW6==3 & Winner15==3)|(generalElectionVoteW6==4 & Winner15==4)|(generalElectionVoteW6==5 & Winner15==5)|(generalElectionVoteW6==7 & Winner15==7))
replace winconW6=. if generalElectionVoteW6==.
label define wincon_label 0 "Loss" 1 "Win"
label values winconW6 wincon_label
tab winconW6 

**# 1c. Coding affective polarization

* We code AP measures at two waves - wave 6 (post-election) and wave 3 (pre-election)

* recoding missing values for party like-dislike

recode likeConW6 likeConW3 likeLabW6 likeLabW3 likeLDW6 likeLDW3 likeUKIPW6 likeUKIPW3 likeGrnW6 likeGrnW3 likeSNPW3 likeSNPW6 likePCW3 likePCW6 (9999=.)

recode likeSNPW3 likeSNPW6 likePCW3 likePCW6 (0/10=.) if countryW6==1
recode likeSNPW3 likeSNPW6 (0/10=.) if countryW6==3

* First - code dummies to indicate whether the R provides pre- and post-election like scores for each party 

bysort id: gen con_nonmiss=1 if likeConW6!=.&likeConW3!=.
bysort id: gen lab_nonmiss=1 if likeLabW6!=.&likeLabW3!=.
bysort id: gen ld_nonmiss=1 if likeLDW6!=.&likeLDW3!=.
bysort id: gen ukip_nonmiss=1 if likeUKIPW6!=.&likeUKIPW3!=.
bysort id: gen green_nonmiss=1 if likeGrnW6!=.&likeGrnW3!=.
bysort id: gen snp_nonmiss=1 if likeSNPW6!=.&likeSNPW3!=.
bysort id: gen pc_nonmiss=1 if likePCW6!=.&likePCW3!=.

* then, identify the number of parties that each individual provide like scores for at both waves

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding the number of parties that each individual provides a like score for at W6

egen likescore_n_eng=rownonmiss(likeConW6 likeLabW6 likeLDW6 likeUKIPW6 likeGrnW6) if countryW6==1
egen likescore_n_sco=rownonmiss(likeConW6 likeLabW6 likeLDW6 likeUKIPW6 likeGrnW6 likeSNPW6) if countryW6==2
egen likescore_n_wal=rownonmiss(likeConW6 likeLabW6 likeLDW6 likeUKIPW6 likeGrnW6 likePCW6) if countryW6==3

gen likescore_n_w6=likescore_n_eng if countryW6==1
replace likescore_n_w6=likescore_n_sco if countryW6==2
replace likescore_n_w6=likescore_n_wal if countryW6==3

tab likescore_n_w6

** Code at W3

drop likescore_n_eng likescore_n_sco likescore_n_wal

egen likescore_n_eng=rownonmiss(likeConW3 likeLabW3 likeLDW3 likeUKIPW3 likeGrnW3) if countryW6==1
egen likescore_n_sco=rownonmiss(likeConW3 likeLabW3 likeLDW3 likeUKIPW3 likeGrnW3 likeSNPW3) if countryW6==2
egen likescore_n_wal=rownonmiss(likeConW3 likeLabW3 likeLDW3 likeUKIPW3 likeGrnW3 likePCW3) if countryW6==3

gen likescore_n_w3=likescore_n_eng if countryW6==1
replace likescore_n_w3=likescore_n_sco if countryW6==2
replace likescore_n_w3=likescore_n_wal if countryW6==3

tab likescore_n_w3

drop likescore_n_eng likescore_n_sco likescore_n_wal

tab likescore_n_w3 
tab likescore_n_w6 


*** Calculating the AP scores without listwise at W6

* First, calculate the weighted mean like score for each party like score. 

gen vsw_likecon=0.409*likeConW6 if countryW6==1
replace vsw_likecon=0.149*likeConW6 if countryW6==2
replace vsw_likecon=0.272*likeConW6 if countryW6==3

gen vsw_likelab=0.316*likeLabW6 if countryW6==1
replace vsw_likelab=0.304*likeLabW6 if countryW6==2
replace vsw_likelab=0.369*likeLabW6 if countryW6==3

gen vsw_likeld=0.082*likeLDW6 if countryW6==1
replace vsw_likeld=0.075*likeLDW6 if countryW6==2
replace vsw_likeld=0.065*likeLDW6 if countryW6==3

gen vsw_likeukip=0.141*likeUKIPW6 if countryW6==1
replace vsw_likeukip=0.016*likeUKIPW6 if countryW6==2
replace vsw_likeukip=0.136*likeUKIPW6 if countryW6==3

gen vsw_likegrn=0.042*likeGrnW6 if countryW6==1
replace vsw_likegrn=0.038*likeGrnW6 if countryW6==2
replace vsw_likegrn=0.026*likeGrnW6 if countryW6==3

gen vsw_likesnp=0.5*likeSNPW6 if countryW6==2

gen vsw_likepc=0.121*likePCW6 if countryW6==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_w6 if mean_ldl_all!=.

* Then I can compute the affective polarization score 
* I compute a party-specific AP component for each individual. 
* I compute the party-specific variables in three parts, to account for differential vote shares across countries. 

gen ap_con=0.409*((likeConW6 - mean_ldl_all)^2) if countryW6==1
replace ap_con=0.149*((likeConW6 - mean_ldl_all)^2) if countryW6==2
replace ap_con=0.272*((likeConW6 - mean_ldl_all)^2) if countryW6==3

gen ap_lab=0.316*((likeLabW6 - mean_ldl_all)^2) if countryW6==1
replace ap_lab=0.304*((likeLabW6 - mean_ldl_all)^2) if countryW6==2
replace ap_lab=0.369*((likeLabW6 - mean_ldl_all)^2) if countryW6==3

gen ap_ld=0.082*((likeLDW6 - mean_ldl_all)^2) if countryW6==1
replace ap_ld=0.075*((likeLDW6 - mean_ldl_all)^2) if countryW6==2
replace ap_ld=0.065*((likeLDW6 - mean_ldl_all)^2) if countryW6==3

gen ap_ukip=0.141*((likeUKIPW6 - mean_ldl_all)^2) if countryW6==1
replace ap_ukip=0.016*((likeUKIPW6 - mean_ldl_all)^2) if countryW6==2
replace ap_ukip=0.136*((likeUKIPW6 - mean_ldl_all)^2) if countryW6==3

gen ap_grn=0.042*((likeGrnW6 - mean_ldl_all)^2) if countryW6==1
replace ap_grn=0.038*((likeGrnW6 - mean_ldl_all)^2) if countryW6==2
replace ap_grn=0.026*((likeGrnW6 - mean_ldl_all)^2) if countryW6==3

gen ap_snp=0.5*((likeSNPW6 - mean_ldl_all)^2) if countryW6==2

gen ap_pc=0.121*((likePCW6 - mean_ldl_all)^2) if countryW6==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap var

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W6=sqrt(affpol_all)

summ affpol_nlw_W6

* Then - again - tab this with the likescore_n_w6 variable to see if the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_w6 if affpol_nlw_W6!=.

*** Coding at wave 3

* First, calculate the weighted mean like score for each party like score.

drop mean_ldl_all

gen vsw_likecon=0.409*likeConW3 if countryW6==1
replace vsw_likecon=0.149*likeConW3 if countryW6==2
replace vsw_likecon=0.272*likeConW3 if countryW6==3

gen vsw_likelab=0.316*likeLabW3 if countryW6==1
replace vsw_likelab=0.304*likeLabW3 if countryW6==2
replace vsw_likelab=0.369*likeLabW3 if countryW6==3

gen vsw_likeld=0.082*likeLDW3 if countryW6==1
replace vsw_likeld=0.075*likeLDW3 if countryW6==2
replace vsw_likeld=0.065*likeLDW3 if countryW6==3

gen vsw_likeukip=0.141*likeUKIPW3 if countryW6==1
replace vsw_likeukip=0.016*likeUKIPW3 if countryW6==2
replace vsw_likeukip=0.136*likeUKIPW3 if countryW6==3

gen vsw_likegrn=0.042*likeGrnW3 if countryW6==1
replace vsw_likegrn=0.038*likeGrnW3 if countryW6==2
replace vsw_likegrn=0.026*likeGrnW3 if countryW6==3

gen vsw_likesnp=0.5*likeSNPW3 if countryW6==2

gen vsw_likepc=0.121*likePCW3 if countryW6==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_w3 if mean_ldl_all!=.

* Then I can compute the affective polarization score 

gen ap_con=0.409*((likeConW3 - mean_ldl_all)^2) if countryW6==1
replace ap_con=0.149*((likeConW3 - mean_ldl_all)^2) if countryW6==2
replace ap_con=0.272*((likeConW3 - mean_ldl_all)^2) if countryW6==3

gen ap_lab=0.316*((likeLabW3 - mean_ldl_all)^2) if countryW6==1
replace ap_lab=0.304*((likeLabW3 - mean_ldl_all)^2) if countryW6==2
replace ap_lab=0.369*((likeLabW3 - mean_ldl_all)^2) if countryW6==3

gen ap_ld=0.082*((likeLDW3 - mean_ldl_all)^2) if countryW6==1
replace ap_ld=0.075*((likeLDW3 - mean_ldl_all)^2) if countryW6==2
replace ap_ld=0.065*((likeLDW3 - mean_ldl_all)^2) if countryW6==3

gen ap_ukip=0.141*((likeUKIPW3 - mean_ldl_all)^2) if countryW6==1
replace ap_ukip=0.016*((likeUKIPW3 - mean_ldl_all)^2) if countryW6==2
replace ap_ukip=0.136*((likeUKIPW3 - mean_ldl_all)^2) if countryW6==3

gen ap_grn=0.042*((likeGrnW3 - mean_ldl_all)^2) if countryW6==1
replace ap_grn=0.038*((likeGrnW3 - mean_ldl_all)^2) if countryW6==2
replace ap_grn=0.026*((likeGrnW3 - mean_ldl_all)^2) if countryW6==3

gen ap_snp=0.5*((likeSNPW3 - mean_ldl_all)^2) if countryW6==2

gen ap_pc=0.121*((likePCW3 - mean_ldl_all)^2) if countryW6==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap var

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W3=sqrt(affpol_all)

summ affpol_nlw_W3

* Then - again - tab this with the likescore_n_w6 variable to see if the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_w3 if affpol_nlw_W3!=.

* Code a copy of the W6 variable for use with the W4 measure. 

gen affpol_proximate_W6=affpol_nlw_W6

* And now, the final step is to only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

gen affpol_nlw_ch_W6=affpol_nlw_W6-affpol_nlw_W3

replace affpol_nlw_ch_W6=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W3=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W6=. if bw_likescore_n==0|bw_likescore_n==1

summ affpol_nlw_ch_W6 // 11,096 obs

tab bw_likescore_n if affpol_nlw_ch_W6!=. // includes those with less than a full like score set 

save bes_2015_temp_v2.dta, replace

**# 1d. Coding IP like and OP dislike 

use bes_2015_temp_v2.dta, clear 

** In-party like 
* Identifying each R's most-liked party 

** Wave 3.

sort id 
egen IP_like_MLP_W3=rowmax(likeConW3 likeLabW3 likeLDW3 likeUKIPW3 likeGrnW3 likeSNPW3 likePCW3)

** Wave 6

sort id 
egen IP_like_MLP_W6=rowmax(likeConW6 likeLabW6 likeLDW6 likeUKIPW6 likeGrnW6 likeSNPW6 likePCW6)

* Coding the change score

gen IP_like_ch_W6=IP_like_MLP_W6-IP_like_MLP_W3

summ IP_like_MLP_W3
summ IP_like_MLP_W6
summ IP_like_ch_W6

* Create copy of W6 var for coding more proximate measure 

gen IP_like_proximate_W6=IP_like_MLP_W6

* Again retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

replace IP_like_MLP_W3=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_MLP_W6=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ch_W6=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_MLP_W3
summ IP_like_MLP_W6
summ IP_like_ch_W6

** Out-party dislike - single party. 

* Closest party in consituency (if one won in constituency), party one lost to in constituency (if one lost).

* Wave 3. If they lost: 

gen OP_dislike_CON_W3 = .

local partylike likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W3 = `var' if winconW6 == 0 & Winner15 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W3 = `var' if winconW6 == 1 & Second15 == `i'
}

* Wave 6. If they lost: 

gen OP_dislike_CON_W6 = .

local partylike likeConW6 likeLabW6 likeLDW6 likeSNPW6 likePCW6 likeUKIPW6 likeGrnW6

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W6 = `var' if winconW6 == 0 & Winner15 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W6 = `var' if winconW6 == 1 & Second15 == `i'
}

summ OP_dislike_CON_W3
summ OP_dislike_CON_W6

* Reverse coding

gen OP_dislike_CON_R_W3=(0-OP_dislike_CON_W3)+10
gen OP_dislike_S_PE_W6=(0-OP_dislike_CON_W6)+10

* gen change score

gen OP_dislike_S_ch_W6=OP_dislike_S_PE_W6-OP_dislike_CON_R_W3

summ OP_dislike_CON_R_W3
summ OP_dislike_S_PE_W6
summ OP_dislike_S_ch_W6

tab likescore_n_w3 if OP_dislike_S_ch_W6!=.

** Out-party dislike - multi-party 

* This is a measure of feeling toward all parties, other than the most-liked party. 

* Wave 3

egen total_like=rowtotal(likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3)

egen total_parties=rownonmiss(likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3)

bysort id: gen OP_dislike_multi_W3=(total_like-IP_like_MLP_W3)/(total_parties-1)

summ OP_dislike_multi_W3

drop total_like total_parties

*Wave 6:

egen total_like=rowtotal(likeConW6 likeLabW6 likeLDW6 likeSNPW6 likePCW6 likeUKIPW6 likeGrnW6)

egen total_parties=rownonmiss(likeConW6 likeLabW6 likeLDW6 likeSNPW6 likePCW6 likeUKIPW6 likeGrnW6)

bysort id: gen OP_dislike_multi_W6=(total_like-IP_like_proximate_W6)/(total_parties-1)

summ OP_dislike_multi_W6

drop total_like total_parties

* Finally, we reverse code this measure

gen OP_multi_W6=(0-OP_dislike_multi_W6)+10
gen OP_multi_W3=(0-OP_dislike_multi_W3)+10

summ OP_multi_W3
summ OP_multi_W6

gen OP_dislike_M_ch_W6=OP_multi_W6-OP_multi_W3

summ OP_multi_W3
summ OP_multi_W6
summ OP_dislike_M_ch_W6

tab likescore_n_w3 if OP_dislike_M_ch_W6!=.

* Create copy of W6 measure for coding more proximate like score 

gen OP_multi_proximate_W6=OP_multi_W6

replace OP_multi_W3=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_multi_W6=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ch_W6=. if bw_likescore_n==0|bw_likescore_n==1

tab likescore_n_w3 if OP_dislike_M_ch_W6!=.

**# 1e. Coding alternative AP measures 

*** Alternative measures for PID 

** In-party like 

* Wave 3

tab pid_wsqueeze_w3

gen IP_like_PID_W3=likeConW3 if pid_wsqueeze_w3==1
replace IP_like_PID_W3=likeLabW3 if pid_wsqueeze_w3==2
replace IP_like_PID_W3=likeLDW3 if pid_wsqueeze_w3==3
replace IP_like_PID_W3=likeSNPW3 if pid_wsqueeze_w3==4
replace IP_like_PID_W3=likePCW3 if pid_wsqueeze_w3==5
replace IP_like_PID_W3=likeUKIPW3 if pid_wsqueeze_w3==6
replace IP_like_PID_W3=likeGrnW3 if pid_wsqueeze_w3==7

* Wave 6 

gen IP_like_PID_W6=likeConW6 if pid_wsqueeze_w3==1
replace IP_like_PID_W6=likeLabW6 if pid_wsqueeze_w3==2
replace IP_like_PID_W6=likeLDW6 if pid_wsqueeze_w3==3
replace IP_like_PID_W6=likeSNPW6 if pid_wsqueeze_w3==4
replace IP_like_PID_W6=likePCW6 if pid_wsqueeze_w3==5
replace IP_like_PID_W6=likeUKIPW6 if pid_wsqueeze_w3==6
replace IP_like_PID_W6=likeGrnW6 if pid_wsqueeze_w3==7

* Coding change score 

gen IP_like_PID_ch_W6=IP_like_PID_W6-IP_like_PID_W3
replace IP_like_PID_ch_W6=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

tab pid_wsqueeze_w3

replace IP_like_PID_ch_W6=IP_like_ch_W6 if pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace IP_like_PID_W3=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_PID_W3=IP_like_MLP_W3 if pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999

sum IP_like_PID_W3
sum IP_like_PID_ch_W6 // 10,896 obs 
sum IP_like_PID_ch_W6 if pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999

** Out-party dislike multi 

* Wave 3

egen total_like=rowtotal(likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3)

egen total_parties=rownonmiss(likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3)

bysort id: gen OP_dislike_M_PID_W3=(total_like-IP_like_PID_W3)/(total_parties-1)

summ OP_dislike_M_PID_W3

drop total_like total_parties

*Wave 6:

egen total_like=rowtotal(likeConW6 likeLabW6 likeLDW6 likeSNPW6 likePCW6 likeUKIPW6 likeGrnW6)

egen total_parties=rownonmiss(likeConW6 likeLabW6 likeLDW6 likeSNPW6 likePCW6 likeUKIPW6 likeGrnW6)

bysort id: gen OP_dislike_M_PID_W6=(total_like-IP_like_PID_W6)/(total_parties-1)

summ OP_dislike_M_PID_W6

drop total_like total_parties

* Reverse coding 

gen OP_multi_PID_W6=(0-OP_dislike_M_PID_W6)+10
gen OP_multi_PID_W3=(0-OP_dislike_M_PID_W3)+10

sum OP_multi_PID_W6
sum OP_multi_PID_W3

* Coding change score 

gen OP_disike_M_PID_ch_W6=OP_multi_PID_W6-OP_multi_PID_W3
replace OP_disike_M_PID_ch_W6=. if bw_likescore_n==0|bw_likescore_n==1

replace OP_multi_PID_W3=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

tab pid_wsqueeze_w3

replace OP_disike_M_PID_ch_W6=OP_dislike_M_ch_W6 if pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace OP_multi_PID_W3=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_multi_PID_W3=OP_multi_W3 if pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999

sum OP_multi_PID_W3
sum OP_disike_M_PID_ch_W6 // 10,896 obs

** Reiljan API index

* Begin by identifying respondent PID

tab partyIdW6
tab partyIdSqueezeW6

gen API_pid_2015=1 if partyIdW6==1|partyIdSqueezeW6==1
replace API_pid_2015=2 if partyIdW6==2|partyIdSqueezeW6==2
replace API_pid_2015=3 if partyIdW6==3|partyIdSqueezeW6==3
replace API_pid_2015=4 if partyIdW6==4|partyIdSqueezeW6==4
replace API_pid_2015=5 if partyIdW6==5|partyIdSqueezeW6==5
replace API_pid_2015=6 if partyIdW6==6|partyIdSqueezeW6==6
replace API_pid_2015=7 if partyIdW6==7|partyIdSqueezeW6==7
replace API_pid_2015=8 if partyIdW6==8|partyIdSqueezeW6==8|partyIdW6==9|partyIdSqueezeW6==9
tab API_pid_2015

label define API_pid_label 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other"
label values API_pid_2015 API_pid_label

gen IP_like_W3=likeConW3 if API_pid_2015==1
replace IP_like_W3=likeLabW3 if API_pid_2015==2
replace IP_like_W3=likeLDW3 if API_pid_2015==3
replace IP_like_W3=likeSNPW3 if API_pid_2015==4
replace IP_like_W3=likePCW3 if API_pid_2015==5
replace IP_like_W3=likeUKIPW3 if API_pid_2015==6
replace IP_like_W3=likeGrnW3 if API_pid_2015==7

// Coding VS scores

gen ConVsENG_2015=0.409
gen LabVsENG_2015=0.316
gen LDVsENG_2015=0.082
gen UKIPVsENG_2015=0.141
gen GrnVsENG_2015=0.042

gen ConVsSCO_2015=0.149
gen LabVsSCO_2015=0.243
gen LDVsSCO_2015=0.075
gen SNPVsSCO_2015=0.5
gen UKIPVsSCO_2015=0.016
gen GrnVsSCO_2015=0.013

gen ConVsWAL_2015=0.272
gen LabVsWAL_2015=0.369
gen LDVsWAL_2015=0.065
gen PCVsWAL_2015=0.121
gen UKIPVsWAL_2015=0.136
gen GrnVsWAL_2015=0.026

** Coding W3

*Conservatives:

gen AP_Con_ENG_W3=((IP_like_W3-likeLabW3)*(LabVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W3-likeLDW3)*(LDVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsENG_2015/(1-ConVsENG_2015))) if API_pid_2015==1&countryW3==1

gen AP_Con_SCO_W3=((IP_like_W3-likeLabW3)*(LabVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W3-likeLDW3)*(LDVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W3-likeSNPW3)*(SNPVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsSCO_2015/(1-ConVsSCO_2015))) if API_pid_2015==1&countryW3==2

gen AP_Con_WAL_W3=((IP_like_W3-likeLabW3)*(LabVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W3-likeLDW3)*(LDVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W3-likePCW3)*(PCVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsWAL_2015/(1-ConVsWAL_2015))) if API_pid_2015==1&countryW3==3

*Labour:

gen AP_Lab_ENG_W3=((IP_like_W3-likeConW3)*(ConVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W3-likeLDW3)*(LDVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsENG_2015/(1-LabVsENG_2015))) if API_pid_2015==2&countryW3==1

gen AP_Lab_SCO_W3=((IP_like_W3-likeConW3)*(ConVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W3-likeLDW3)*(LDVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W3-likeSNPW3)*(SNPVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsSCO_2015/(1-LabVsSCO_2015))) if API_pid_2015==2&countryW3==2

gen AP_Lab_WAL_W3=((IP_like_W3-likeConW3)*(ConVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W3-likeLDW3)*(LDVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W3-likePCW3)*(PCVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsWAL_2015/(1-LabVsWAL_2015))) if API_pid_2015==2&countryW3==3

*Lib Dems:

gen AP_LD_ENG_W3=((IP_like_W3-likeConW3)*(ConVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W3-likeLabW3)*(LabVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsENG_2015/(1-LDVsENG_2015))) if API_pid_2015==3&countryW3==1

gen AP_LD_SCO_W3=((IP_like_W3-likeConW3)*(ConVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W3-likeLabW3)*(LabVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W3-likeSNPW3)*(SNPVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsSCO_2015/(1-LDVsSCO_2015))) if API_pid_2015==3&countryW3==2

gen AP_LD_WAL_W3=((IP_like_W3-likeConW3)*(ConVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W3-likeLabW3)*(LabVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W3-likePCW3)*(PCVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsWAL_2015/(1-LDVsWAL_2015))) if API_pid_2015==3&countryW3==3

*SNP:

gen AP_SNP_W3=((IP_like_W3-likeConW3)*(ConVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W3-likeLabW3)*(LabVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W3-likeLDW3)*(LDVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsSCO_2015/(1-SNPVsSCO_2015))) if API_pid_2015==4&countryW3==2
tab AP_SNP_W3

*PC:

gen AP_PC_W3=((IP_like_W3-likeConW3)*(ConVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W3-likeLabW3)*(LabVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W3-likeLDW3)*(LDVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsWAL_2015/(1-PCVsWAL_2015))) if API_pid_2015==5&countryW3==3
tab AP_PC_W3

*UKIP:

gen AP_UKIP_ENG_W3=((IP_like_W3-likeConW3)*(ConVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W3-likeLabW3)*(LabVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W3-likeLDW3)*(LDVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsENG_2015/(1-UKIPVsENG_2015))) if API_pid_2015==6&countryW3==1

gen AP_UKIP_SCO_W3=((IP_like_W3-likeConW3)*(ConVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W3-likeLabW3)*(LabVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W3-likeSNPW3)*(SNPVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W3-likeLDW3)*(LDVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsSCO_2015/(1-UKIPVsSCO_2015))) if API_pid_2015==6&countryW3==2

gen AP_UKIP_WAL_W3=((IP_like_W3-likeConW3)*(ConVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W3-likeLabW3)*(LabVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W3-likePCW3)*(PCVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W3-likeLDW3)*(LDVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W3-likeGrnW3)*(GrnVsWAL_2015/(1-UKIPVsWAL_2015))) if API_pid_2015==6&countryW3==3

*Greens:

gen AP_Grn_ENG_W3=((IP_like_W3-likeConW3)*(ConVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W3-likeLabW3)*(LabVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W3-likeLDW3)*(LDVsENG_2015/(1-GrnVsENG_2015))) if API_pid_2015==7&countryW3==1

gen AP_Grn_SCO_W3=((IP_like_W3-likeConW3)*(ConVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W3-likeLabW3)*(LabVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W3-likeSNPW3)*(SNPVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W3-likeLDW3)*(LDVsSCO_2015/(1-GrnVsSCO_2015))) if API_pid_2015==7&countryW3==2

gen AP_Grn_WAL_W3=((IP_like_W3-likeConW3)*(ConVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W3-likeLabW3)*(LabVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W3-likePCW3)*(PCVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W3-likeUKIPW3)*(UKIPVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W3-likeLDW3)*(LDVsWAL_2015/(1-GrnVsWAL_2015))) if API_pid_2015==7&countryW3==3

*AP: 

gen AP_R_W3=.
replace AP_R_W3=AP_Con_ENG_W3 if countryW3==1&API_pid_2015==1
replace AP_R_W3=AP_Con_SCO_W3 if countryW3==2&API_pid_2015==1
replace AP_R_W3=AP_Con_WAL_W3 if countryW3==3&API_pid_2015==1
replace AP_R_W3=AP_Lab_ENG_W3 if countryW3==1&API_pid_2015==2
replace AP_R_W3=AP_Lab_SCO_W3 if countryW3==2&API_pid_2015==2
replace AP_R_W3=AP_Lab_WAL_W3 if countryW3==3&API_pid_2015==2
replace AP_R_W3=AP_LD_ENG_W3 if countryW3==1&API_pid_2015==3
replace AP_R_W3=AP_LD_SCO_W3 if countryW3==2&API_pid_2015==3
replace AP_R_W3=AP_LD_WAL_W3 if countryW3==3&API_pid_2015==3
replace AP_R_W3=AP_SNP_W3 if API_pid_2015==4
replace AP_R_W3=AP_PC_W3 if API_pid_2015==5
replace AP_R_W3=AP_UKIP_ENG_W3 if countryW3==1&API_pid_2015==6
replace AP_R_W3=AP_UKIP_SCO_W3 if countryW3==2&API_pid_2015==6
replace AP_R_W3=AP_UKIP_WAL_W3 if countryW3==3&API_pid_2015==6
replace AP_R_W3=AP_Grn_ENG_W3 if countryW3==1&API_pid_2015==7
replace AP_R_W3=AP_Grn_SCO_W3 if countryW3==2&API_pid_2015==7
replace AP_R_W3=AP_Grn_WAL_W3 if countryW3==3&API_pid_2015==7

summ AP_R_W3

// AP scores - W6

gen IP_like_W6=likeConW6 if API_pid_2015==1
replace IP_like_W6=likeLabW6 if API_pid_2015==2
replace IP_like_W6=likeLDW6 if API_pid_2015==3
replace IP_like_W6=likeSNPW6 if API_pid_2015==4
replace IP_like_W6=likePCW6 if API_pid_2015==5
replace IP_like_W6=likeUKIPW6 if API_pid_2015==6
replace IP_like_W6=likeGrnW6 if API_pid_2015==7

*Conservatives:

gen AP_Con_ENG_W6=((IP_like_W6-likeLabW6)*(LabVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W6-likeLDW6)*(LDVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsENG_2015/(1-ConVsENG_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsENG_2015/(1-ConVsENG_2015))) if API_pid_2015==1&countryW6==1

gen AP_Con_SCO_W6=((IP_like_W6-likeLabW6)*(LabVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W6-likeLDW6)*(LDVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W6-likeSNPW6)*(SNPVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsSCO_2015/(1-ConVsSCO_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsSCO_2015/(1-ConVsSCO_2015))) if API_pid_2015==1&countryW6==2

gen AP_Con_WAL_W6=((IP_like_W6-likeLabW6)*(LabVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W6-likeLDW6)*(LDVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W6-likePCW6)*(PCVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsWAL_2015/(1-ConVsWAL_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsWAL_2015/(1-ConVsWAL_2015))) if API_pid_2015==1&countryW6==3

*Labour:

gen AP_Lab_ENG_W6=((IP_like_W6-likeConW6)*(ConVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W6-likeLDW6)*(LDVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsENG_2015/(1-LabVsENG_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsENG_2015/(1-LabVsENG_2015))) if API_pid_2015==2&countryW6==1

gen AP_Lab_SCO_W6=((IP_like_W6-likeConW6)*(ConVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W6-likeLDW6)*(LDVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W6-likeSNPW6)*(SNPVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsSCO_2015/(1-LabVsSCO_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsSCO_2015/(1-LabVsSCO_2015))) if API_pid_2015==2&countryW6==2

gen AP_Lab_WAL_W6=((IP_like_W6-likeConW6)*(ConVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W6-likeLDW6)*(LDVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W6-likePCW6)*(PCVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsWAL_2015/(1-LabVsWAL_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsWAL_2015/(1-LabVsWAL_2015))) if API_pid_2015==2&countryW6==3

*Lib Dems:

gen AP_LD_ENG_W6=((IP_like_W6-likeConW6)*(ConVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W6-likeLabW6)*(LabVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsENG_2015/(1-LDVsENG_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsENG_2015/(1-LDVsENG_2015))) if API_pid_2015==3&countryW6==1

gen AP_LD_SCO_W6=((IP_like_W6-likeConW6)*(ConVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W6-likeLabW6)*(LabVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W6-likeSNPW6)*(SNPVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsSCO_2015/(1-LDVsSCO_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsSCO_2015/(1-LDVsSCO_2015))) if API_pid_2015==3&countryW6==2

gen AP_LD_WAL_W6=((IP_like_W6-likeConW6)*(ConVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W6-likeLabW6)*(LabVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W6-likePCW6)*(PCVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsWAL_2015/(1-LDVsWAL_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsWAL_2015/(1-LDVsWAL_2015))) if API_pid_2015==3&countryW6==3

*SNP:

gen AP_SNP_W6=((IP_like_W6-likeConW6)*(ConVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W6-likeLabW6)*(LabVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W6-likeLDW6)*(LDVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsSCO_2015/(1-SNPVsSCO_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsSCO_2015/(1-SNPVsSCO_2015))) if API_pid_2015==4&countryW6==2
tab AP_SNP_W6

*PC:

gen AP_PC_W6=((IP_like_W6-likeConW6)*(ConVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W6-likeLabW6)*(LabVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W6-likeLDW6)*(LDVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsWAL_2015/(1-PCVsWAL_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsWAL_2015/(1-PCVsWAL_2015))) if API_pid_2015==5&countryW6==3
tab AP_PC_W6

*UKIP:

gen AP_UKIP_ENG_W6=((IP_like_W6-likeConW6)*(ConVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W6-likeLabW6)*(LabVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W6-likeLDW6)*(LDVsENG_2015/(1-UKIPVsENG_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsENG_2015/(1-UKIPVsENG_2015))) if API_pid_2015==6&countryW6==1

gen AP_UKIP_SCO_W6=((IP_like_W6-likeConW6)*(ConVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W6-likeLabW6)*(LabVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W6-likeSNPW6)*(SNPVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W6-likeLDW6)*(LDVsSCO_2015/(1-UKIPVsSCO_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsSCO_2015/(1-UKIPVsSCO_2015))) if API_pid_2015==6&countryW6==2

gen AP_UKIP_WAL_W6=((IP_like_W6-likeConW6)*(ConVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W6-likeLabW6)*(LabVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W6-likePCW6)*(PCVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W6-likeLDW6)*(LDVsWAL_2015/(1-UKIPVsWAL_2015)))+((IP_like_W6-likeGrnW6)*(GrnVsWAL_2015/(1-UKIPVsWAL_2015))) if API_pid_2015==6&countryW6==3

*Greens:

gen AP_Grn_ENG_W6=((IP_like_W6-likeConW6)*(ConVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W6-likeLabW6)*(LabVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsENG_2015/(1-GrnVsENG_2015)))+((IP_like_W6-likeLDW6)*(LDVsENG_2015/(1-GrnVsENG_2015))) if API_pid_2015==7&countryW6==1

gen AP_Grn_SCO_W6=((IP_like_W6-likeConW6)*(ConVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W6-likeLabW6)*(LabVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W6-likeSNPW6)*(SNPVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsSCO_2015/(1-GrnVsSCO_2015)))+((IP_like_W6-likeLDW6)*(LDVsSCO_2015/(1-GrnVsSCO_2015))) if API_pid_2015==7&countryW6==2

gen AP_Grn_WAL_W6=((IP_like_W6-likeConW6)*(ConVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W6-likeLabW6)*(LabVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W6-likePCW6)*(PCVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W6-likeUKIPW6)*(UKIPVsWAL_2015/(1-GrnVsWAL_2015)))+((IP_like_W6-likeLDW6)*(LDVsWAL_2015/(1-GrnVsWAL_2015))) if API_pid_2015==7&countryW6==3

*AP: 

gen AP_R_W6=.
replace AP_R_W6=AP_Con_ENG_W6 if countryW6==1&API_pid_2015==1
replace AP_R_W6=AP_Con_SCO_W6 if countryW6==2&API_pid_2015==1
replace AP_R_W6=AP_Con_WAL_W6 if countryW6==3&API_pid_2015==1
replace AP_R_W6=AP_Lab_ENG_W6 if countryW6==1&API_pid_2015==2
replace AP_R_W6=AP_Lab_SCO_W6 if countryW6==2&API_pid_2015==2
replace AP_R_W6=AP_Lab_WAL_W6 if countryW6==3&API_pid_2015==2
replace AP_R_W6=AP_LD_ENG_W6 if countryW6==1&API_pid_2015==3
replace AP_R_W6=AP_LD_SCO_W6 if countryW6==2&API_pid_2015==3
replace AP_R_W6=AP_LD_WAL_W6 if countryW6==3&API_pid_2015==3
replace AP_R_W6=AP_SNP_W6 if API_pid_2015==4
replace AP_R_W6=AP_PC_W6 if API_pid_2015==5
replace AP_R_W6=AP_UKIP_ENG_W6 if countryW6==1&API_pid_2015==6
replace AP_R_W6=AP_UKIP_SCO_W6 if countryW6==2&API_pid_2015==6
replace AP_R_W6=AP_UKIP_WAL_W6 if countryW6==3&API_pid_2015==6
replace AP_R_W6=AP_Grn_ENG_W6 if countryW6==1&API_pid_2015==7
replace AP_R_W6=AP_Grn_SCO_W6 if countryW6==2&API_pid_2015==7
replace AP_R_W6=AP_Grn_WAL_W6 if countryW6==3&API_pid_2015==7

summ AP_R_W3
summ AP_R_W6

gen AP_R_ch_W6=AP_R_W6-AP_R_W3

*** Wagner weighted mean-distance measure 

** wave 3

* first identify most-liked party 

egen MLP_W3=rowmax(likeConW3 likeLabW3 likeLDW3 likeUKIPW3 likeGrnW3 likeSNPW3 likePCW3)

summ MLP_W3

* Then we calculate the distance scores. We again initially code different measures for each country before combining, to account for differences in party system structure. 

gen AP_distance_ENG_W3=sqrt((ConVsENG_2015*((likeConW3-MLP_W3)^2))+(LabVsENG_2015*((likeLabW3-MLP_W3)^2))+(LDVsENG_2015*((likeLDW3-MLP_W3)^2))+(UKIPVsENG_2015*((likeUKIPW3-MLP_W3)^2))+(GrnVsENG_2015*((likeGrnW3-MLP_W3)^2))) if countryW3==1
gen AP_distance_SCO_W3=sqrt((ConVsSCO_2015*((likeConW3-MLP_W3)^2))+(LabVsSCO_2015*((likeLabW3-MLP_W3)^2))+(LDVsSCO_2015*((likeLDW3-MLP_W3)^2))+(UKIPVsSCO_2015*((likeUKIPW3-MLP_W3)^2))+(GrnVsSCO_2015*((likeGrnW3-MLP_W3)^2))+(SNPVsSCO_2015*((likeSNPW3-MLP_W3)^2))) if countryW3==2
gen AP_distance_WAL_W3=sqrt((ConVsWAL_2015*((likeConW3-MLP_W3)^2))+(LabVsWAL_2015*((likeLabW3-MLP_W3)^2))+(LDVsWAL_2015*((likeLDW3-MLP_W3)^2))+(UKIPVsWAL_2015*((likeUKIPW3-MLP_W3)^2))+(GrnVsWAL_2015*((likeGrnW3-MLP_W3)^2))+(PCVsWAL_2015*((likePCW3-MLP_W3)^2))) if countryW3==3

gen AP_distance_W3=.
replace AP_distance_W3=AP_distance_ENG_W3 if countryW3==1
replace AP_distance_W3=AP_distance_SCO_W3 if countryW3==2
replace AP_distance_W3=AP_distance_WAL_W3 if countryW3==3
summ AP_distance_W3

** Wave 6 

* We next repeat the same process at wave 6

egen MLP_W6=rowmax(likeConW6 likeLabW6 likeLDW6 likeUKIPW6 likeGrnW6 likeSNPW6 likePCW6)

gen AP_distance_ENG_W6=sqrt((ConVsENG_2015*((likeConW6-MLP_W6)^2))+(LabVsENG_2015*((likeLabW6-MLP_W6)^2))+(LDVsENG_2015*((likeLDW6-MLP_W6)^2))+(UKIPVsENG_2015*((likeUKIPW6-MLP_W6)^2))+(GrnVsENG_2015*((likeGrnW6-MLP_W6)^2))) if countryW6==1
gen AP_distance_SCO_W6=sqrt((ConVsSCO_2015*((likeConW6-MLP_W6)^2))+(LabVsSCO_2015*((likeLabW6-MLP_W6)^2))+(LDVsSCO_2015*((likeLDW6-MLP_W6)^2))+(UKIPVsSCO_2015*((likeUKIPW6-MLP_W6)^2))+(GrnVsSCO_2015*((likeGrnW6-MLP_W6)^2))+(SNPVsSCO_2015*((likeSNPW6-MLP_W6)^2))) if countryW6==2
gen AP_distance_WAL_W6=sqrt((ConVsWAL_2015*((likeConW6-MLP_W6)^2))+(LabVsWAL_2015*((likeLabW6-MLP_W6)^2))+(LDVsWAL_2015*((likeLDW6-MLP_W6)^2))+(UKIPVsWAL_2015*((likeUKIPW6-MLP_W6)^2))+(GrnVsWAL_2015*((likeGrnW6-MLP_W6)^2))+(PCVsWAL_2015*((likePCW6-MLP_W6)^2))) if countryW6==3

gen AP_distance_W6=.
replace AP_distance_W6=AP_distance_ENG_W6 if countryW6==1
replace AP_distance_W6=AP_distance_SCO_W6 if countryW6==2
replace AP_distance_W6=AP_distance_WAL_W6 if countryW6==3
summ AP_distance_W6

gen AP_distance_ch_W6=AP_distance_W6-AP_distance_W3

**# 1f. Coding wave 4 measures 

* So, I will need to code: wave 4 AP and W4-W6 change score; W4 IP like and W4-W6 change score; wave 4 OP single and W4-W6 change score; wave 4 OP multi and W4-W6 change score. 

** Affective polarization

* recoding missing values for party like-dislike

recode likeConW4 likeLabW4 likeLDW4 likeUKIPW4 likeGrnW4 likeSNPW4 likePCW4 (9999=.)

recode likeSNPW4 likePCW4 (0/10=.) if countryW6==1
recode likeSNPW4 (0/10=.) if countryW6==3

* Coding nonmissing identifiers

drop con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss

bysort id: gen con_nonmiss=1 if likeConW6!=.&likeConW4!=.
bysort id: gen lab_nonmiss=1 if likeLabW6!=.&likeLabW4!=.
bysort id: gen ld_nonmiss=1 if likeLDW6!=.&likeLDW4!=.
bysort id: gen ukip_nonmiss=1 if likeUKIPW6!=.&likeUKIPW4!=.
bysort id: gen green_nonmiss=1 if likeGrnW6!=.&likeGrnW4!=.
bysort id: gen snp_nonmiss=1 if likeSNPW6!=.&likeSNPW4!=.
bysort id: gen pc_nonmiss=1 if likePCW6!=.&likePCW4!=.

* then, identify the number of parties that each individual provide like scores for at both waves

drop bw_likescore_n

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding the number of parties that each individual provides a like score for at W4

egen likescore_n_eng=rownonmiss(likeConW4 likeLabW4 likeLDW4 likeUKIPW4 likeGrnW4) if countryW6==1
egen likescore_n_sco=rownonmiss(likeConW4 likeLabW4 likeLDW4 likeUKIPW4 likeGrnW4 likeSNPW4) if countryW6==2
egen likescore_n_wal=rownonmiss(likeConW4 likeLabW4 likeLDW4 likeUKIPW4 likeGrnW4 likePCW4) if countryW6==3

gen likescore_n_W4=likescore_n_eng if countryW6==1
replace likescore_n_W4=likescore_n_sco if countryW6==2
replace likescore_n_W4=likescore_n_wal if countryW6==3

tab likescore_n_W4

* Already have likescore_n at wave 6

tab likescore_n_w6

*** Calculating the AP scores without listwise at W4

* First, calculate the weighted mean like score for each party like score. 

gen vsw_likecon=0.409*likeConW4 if countryW6==1
replace vsw_likecon=0.149*likeConW4 if countryW6==2
replace vsw_likecon=0.272*likeConW4 if countryW6==3

gen vsw_likelab=0.316*likeLabW4 if countryW6==1
replace vsw_likelab=0.304*likeLabW4 if countryW6==2
replace vsw_likelab=0.369*likeLabW4 if countryW6==3

gen vsw_likeld=0.082*likeLDW4 if countryW6==1
replace vsw_likeld=0.075*likeLDW4 if countryW6==2
replace vsw_likeld=0.065*likeLDW4 if countryW6==3

gen vsw_likeukip=0.141*likeUKIPW4 if countryW6==1
replace vsw_likeukip=0.016*likeUKIPW4 if countryW6==2
replace vsw_likeukip=0.136*likeUKIPW4 if countryW6==3

gen vsw_likegrn=0.042*likeGrnW4 if countryW6==1
replace vsw_likegrn=0.038*likeGrnW4 if countryW6==2
replace vsw_likegrn=0.026*likeGrnW4 if countryW6==3

gen vsw_likesnp=0.5*likeSNPW4 if countryW6==2

gen vsw_likepc=0.121*likePCW4 if countryW6==3

* Then, calculate the mean by summing these scores up 

drop mean_ldl_all 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W4 if mean_ldl_all!=.

** Computing AP scores

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.409*((likeConW4 - mean_ldl_all)^2) if countryW6==1
replace ap_con=0.149*((likeConW4 - mean_ldl_all)^2) if countryW6==2
replace ap_con=0.272*((likeConW4 - mean_ldl_all)^2) if countryW6==3

gen ap_lab=0.316*((likeLabW4 - mean_ldl_all)^2) if countryW6==1
replace ap_lab=0.304*((likeLabW4 - mean_ldl_all)^2) if countryW6==2
replace ap_lab=0.369*((likeLabW4 - mean_ldl_all)^2) if countryW6==3

gen ap_ld=0.082*((likeLDW4 - mean_ldl_all)^2) if countryW6==1
replace ap_ld=0.075*((likeLDW4 - mean_ldl_all)^2) if countryW6==2
replace ap_ld=0.065*((likeLDW4 - mean_ldl_all)^2) if countryW6==3

gen ap_ukip=0.141*((likeUKIPW4 - mean_ldl_all)^2) if countryW6==1
replace ap_ukip=0.016*((likeUKIPW4 - mean_ldl_all)^2) if countryW6==2
replace ap_ukip=0.136*((likeUKIPW4 - mean_ldl_all)^2) if countryW6==3

gen ap_grn=0.042*((likeGrnW4 - mean_ldl_all)^2) if countryW6==1
replace ap_grn=0.038*((likeGrnW4 - mean_ldl_all)^2) if countryW6==2
replace ap_grn=0.026*((likeGrnW4 - mean_ldl_all)^2) if countryW6==3

gen ap_snp=0.5*((likeSNPW4 - mean_ldl_all)^2) if countryW6==2

gen ap_pc=0.121*((likePCW4 - mean_ldl_all)^2) if countryW6==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W4=sqrt(affpol_all)

summ affpol_nlw_W4

* Then - again - tab this with the likescore_n variable make sure the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W4 if affpol_nlw_W4!=.

* And now, the final step is to (a) code the change score and (b) only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

gen affpol_nlw_ich_W6=affpol_proximate_W6-affpol_nlw_W4

replace affpol_nlw_ich_W6=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W4=. if bw_likescore_n==0|bw_likescore_n==1

* Then - compare this to the original variable 

summ affpol_nlw_ich_W6 // 13,175 obs - 2079 more than the less proximate variable
summ affpol_nlw_ch_W6

disp 13175-11096

tab bw_likescore_n if affpol_nlw_ich_W6!=.

** In-party like

sort id 
egen IP_like_MLP_W4=rowmax(likeConW4 likeLabW4 likeLDW4 likeUKIPW4 likeGrnW4 likeSNPW4 likePCW4)

* Coding the change score

gen IP_like_ich_W6=IP_like_proximate_W6-IP_like_MLP_W4

replace IP_like_MLP_W4=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ich_W6=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_ich_W6 // again, 13,175 obs
summ IP_like_ch_W6

** Out-party dislike
* Single
* If they lost: 

gen OP_dislike_CON_W4 = .

local partylike likeConW4 likeLabW4 likeLDW4 likeSNPW4 likePCW4 likeUKIPW4 likeGrnW4

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W4 = `var' if winconW6 == 0 & Winner15 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W4 = `var' if winconW6 == 1 & Second15 == `i'
}

summ OP_dislike_CON_W4

gen OP_dislike_CON_R_W4=(0-OP_dislike_CON_W4)+10
summ OP_dislike_CON_R_W4

gen OP_dislike_S_ich_W6=OP_dislike_S_PE_W6-OP_dislike_CON_R_W4

summ OP_dislike_S_ich_W6 // 12,251 obs - 1878 more than less proximate measure
summ OP_dislike_S_ch_W6

disp 12251-10373

** Out-party dislike - multi-party 

egen total_like=rowtotal(likeConW4 likeLabW4 likeLDW4 likeSNPW4 likePCW4 likeUKIPW4 likeGrnW4)

egen total_parties=rownonmiss(likeConW4 likeLabW4 likeLDW4 likeSNPW4 likePCW4 likeUKIPW4 likeGrnW4)

bysort id: gen OP_dislike_multi_W4=(total_like-IP_like_MLP_W4)/(total_parties-1)

summ OP_dislike_multi_W4

drop total_like total_parties

* Finally, we reverse code this measure

gen OP_multi_W4=(0-OP_dislike_multi_W4)+10

summ OP_multi_W4

gen OP_dislike_M_ich_W6=OP_multi_proximate_W6-OP_multi_W4

replace OP_multi_W4=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ich_W6=. if bw_likescore_n==0|bw_likescore_n==1

summ OP_dislike_M_ich_W6 // again, 13,175 obs

**# 1g.  Merge with campaign spending data 

* first, save the data 

save bes_2015_temp_v3.dta, replace

use bes_2015_temp_v3.dta, clear

* Coding Constituency var for matching

decode pconW6, gen(Constituency)

drop _merge 

* Complete merge

merge m:1 Constituency using campaign_spending_2015.dta

* Calculate matching rate 

disp 29217/(29217+886)*100

* 97% matching rate 

tab _merge

keep if _merge==3

** Adjusting spending scores by electorate size

gen all_party_spend_W6=A_con_spend_W6/Electorate15

summ A_con_spend_W6
summ all_party_spend_W6

replace Conservative_spend_W6=Conservative_spend_W6/Electorate15
replace Labour_spend_W6=Labour_spend_W6/Electorate15
replace LD_spend_W6=LD_spend_W6/Electorate15
replace SNP_spend_W6=SNP_spend_W6/Electorate15
replace PC_spend_W6=PC_spend_W6/Electorate15
replace UKIP_spend_W6=UKIP_spend_W6/Electorate15
replace Green_spend_W6=Green_spend_W6/Electorate15

summ Conservative_spend_W6

save bes_2015_temp_v4.dta, replace

** Calculating in-party spending

use bes_2015_temp_v4.dta, clear

summ affpol_nlw_ch_W6

* first, identify the identity of the in-party - R's most liked party

* first thing I need to do is identify those individuals who have multiple parties with the same max like score


gen match_count_W3 = ///
    cond(!missing(MLP_W3) & MLP_W3 == likeConW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likeLabW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likeLDW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likeGrnW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likeUKIPW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likeSNPW3, 1, 0) + ///
    cond(!missing(MLP_W3) & MLP_W3 == likePCW3, 1, 0)
	
tab match_count_W3

* Now i can code an in-party identifier, using the same numeric structure as for other variables. Con=1; Lab=2; LD=3; SNP=4; PC = 5; UKIP = 6; Green = 7; Other = 8; Brexit= 9.

* First, generate the variable
gen IP_id = .

* Then define the party variables and corresponding ID values
local parties likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3
local ids 1 2 3 4 5 6 7

* Coding a loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    
    replace IP_id = `id' if MLP_W3 == `var' & match_count_W3 == 1
}

* for those individuals with multiple parties with the same max like score, I use PID to differentiate. 

tab partyIdW3
tab partyIdSqueezeW3 if partyIdW3>8

replace IP_id=partyIdW3 if match_count_W3>1&partyIdW3<8
replace IP_id=partyIdSqueezeW3 if match_count_W3>1&partyIdW3>8&partyIdSqueezeW3<8

* This is not a perfect solution - there are still some individuals without a in-party, becuase they have no PID. 

tab match_count_W3 if match_count_W3>1&IP_id==.

* 547 at this wave 

tab IP_id

** Coding the IP spend var 

* generating the variable 

gen IP_spend_W6=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W6 Labour_spend_W6 LD_spend_W6 SNP_spend_W6 PC_spend_W6 UKIP_spend_W6 Green_spend_W6
local ids 1 2 3 4 5 6 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_W6 = `var' if IP_id == `id'
}

summ IP_spend_W6

browse

** Coding out-party spend 

bysort id: gen OP_spend_W6=all_party_spend_W6-IP_spend_W6

summ OP_spend_W6

*** PID Spending measures 

* First, code a new in-party ID var that is the in-party for those with a PID; and the MLP for those without a PID. 

tab pid_wsqueeze_w3

gen IP_PID_id=pid_wsqueeze_w3

* Then define the party variables and corresponding ID values
local parties likeConW3 likeLabW3 likeLDW3 likeSNPW3 likePCW3 likeUKIPW3 likeGrnW3
local ids 1 2 3 4 5 6 7

* Coding a loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    
    replace IP_PID_id = `id' if MLP_W3 == `var' & (pid_wsqueeze_w3==10|pid_wsqueeze_w3==9999)
}

tab IP_PID_id

** Coding IP spend. First, generate the variable 

gen IP_spend_PID_W6=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W6 Labour_spend_W6 LD_spend_W6 SNP_spend_W6 PC_spend_W6 UKIP_spend_W6 Green_spend_W6
local ids 1 2 3 4 5 6 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_PID_W6 = `var' if IP_PID_id == `id'
}

summ IP_spend_PID_W6
summ IP_spend_W6

** Coding out-party spend 

bysort id: gen OP_spend_PID_W6=all_party_spend_W6-IP_spend_PID_W6

summ OP_spend_W6
summ OP_spend_PID_W6

**# 1h. Coding party contact data 

tab IP_id

tab partyContact1W6

tab partyContactConW6
tab partyContactLabW6
tab partyContactLDW6
tab partyContactSNPW6
tab partyContactPCW6
tab partyContactUKIPW6
tab partyContactGrnW6

recode partyContactConW6 partyContactLabW6 partyContactLDW6 partyContactSNPW6 partyContactPCW6 partyContactUKIPW6 partyContactGrnW6 (9999=.)

* Coding in-party contact 

gen IP_contact_W6=0 if partyContact1W6==0
replace IP_contact_W6=1 if partyContactConW6==1&IP_id==1
replace IP_contact_W6=1 if partyContactLabW6==1&IP_id==2
replace IP_contact_W6=1 if partyContactLDW6==1&IP_id==3
replace IP_contact_W6=1 if partyContactSNPW6==1&IP_id==4
replace IP_contact_W6=1 if partyContactPCW6==1&IP_id==5
replace IP_contact_W6=1 if IP_id==6&partyContactUKIPW6==1
replace IP_contact_W6=1 if IP_id==7&partyContactGrnW6==1

tab IP_contact_W6

* Coding out-party contact 
* Going to use the self-reported contact variables for specific out-parties to code this, rather than inferring from the in-party variable, because I think it is important that the respondent specifically reports out-party contact

ssc install ereplace

egen OP_contact_W6=rowtotal(partyContactLabW6 partyContactLDW6 partyContactSNPW6 partyContactPCW6 partyContactUKIPW6 partyContactGrnW6) if IP_id==1
ereplace OP_contact_W6=rowtotal(partyContactConW6 partyContactLDW6 partyContactSNPW6 partyContactPCW6 partyContactUKIPW6 partyContactGrnW6) if IP_id==2
ereplace OP_contact_W6=rowtotal(partyContactConW6 partyContactLabW6 partyContactSNPW6 partyContactPCW6 partyContactUKIPW6 partyContactGrnW6) if IP_id==3
ereplace OP_contact_W6=rowtotal(partyContactConW6 partyContactLabW6 partyContactLDW6 partyContactPCW6 partyContactUKIPW6 partyContactGrnW6) if IP_id==4
ereplace OP_contact_W6=rowtotal(partyContactConW6 partyContactLabW6 partyContactLDW6 partyContactUKIPW6 partyContactGrnW6) if IP_id==5
ereplace OP_contact_W6=rowtotal(partyContactLabW6 partyContactLDW6 partyContactSNPW6 partyContactPCW6 partyContactGrnW6) if IP_id==6
ereplace OP_contact_W6=rowtotal(partyContactConW6 partyContactLabW6 partyContactLDW6 partyContactSNPW6 partyContactPCW6 partyContactUKIPW6) if IP_id==7

tab OP_contact_W6
tab IP_contact_W6
tab IP_id

pwcorr IP_contact_W6 OP_contact_W6

* Tactical voting indicator 

tab generalElectionVoteW6

tab pid_wsqueeze_w3

gen tactical_voteW6=1 if (generalElectionVoteW6!=pid_wsqueeze_w3) & (generalElectionVoteW6<10&pid_wsqueeze_w3<10)
replace tactical_voteW6=0 if (generalElectionVoteW6==pid_wsqueeze_w3) & (generalElectionVoteW6<10&pid_wsqueeze_w3<10)

tab tactical_voteW6

**# 1i. Saving the data and preparing for merge

keep affpol* IP_* OP_* countryW6 genderW6 ethnicW6 ageW6 higheducW6 pid_pre_W6 pid_pre_wsqueeze_W6 pid_strength_W6 wingovW6 winconW6 CI_15 panoW6 eff_parties15 raw_margin_W6 pconW6 id AP_R_* AP_distance* IP_spend_W6 OP_spend_W6 R_con_spend_2015 A_con_spend_W6 all_party_spend_W6 farright_ep_W6 Constituency tactical_voteW6

gen affpol_nlw_PE_W6=affpol_nlw_W6

save bes_2015_temp_v5.dta, replace

**# 2. 2017 - BESIP data coding 

use BES2024_W29_Panel_v29.1.dta, clear

**# 2a. merge the individual-level data with the competitiveness data

merge m:1 panoW13 using CI_2017_v2.dta

drop if CI_17==.

save bes_2017_temp.dta, replace

**# 2b. coding individual-level variables 

*** PID as a moderator

* Binary measure

tab partyIdW10

recode partyIdW10 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_W13)

tab pid_pre_W13

* With the squeeze follow-up

gen pid_wsqueeze_w10=partyIdW10
replace pid_wsqueeze_w10=partyIdSqueezeW10 if partyIdW10==10|partyIdW10==9999
tab pid_wsqueeze_w10

recode pid_wsqueeze_w10 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_wsqueeze_W13)
tab pid_pre_wsqueeze_W13

* Categorical measure

tab partyIdStrengthW10

recode partyIdStrengthW10 (1=4 "Very strong") (2=3 "Fairly Strong") (3=2 "Not very strong") (9999=1 "No PID/Don't know"), gen(pid_strength_W13)
replace pid_strength_W13=1 if pid_pre_W13==0

tab pid_strength_W13

*** Controls 

** Education

recode p_edlevelW13 (4/5=1 "Degree") (0/3=0 "No degree"), gen (higheducW13)

** Age

summ ageW13

** Ethnicity 

recode p_ethnicityW13 (1=1 "White British") (else=0 "Other"), gen (ethnicW13)

** country 

tab countryW13

** Gender 

gen genderW13=.
replace genderW13=0 if gender==1 & wave13==1
replace genderW13 = 1 if gender==2 & wave13==1

** National election outcome

tab generalElectionVoteW13
recode generalElectionVoteW13 (1=1) (2/9=0) (9999=.), gen(wingovW13)
tab wingovW13

label define win_label 0 "Loss" 1 "Win"
label values wingovW13 win_label
tab wingovW13

** Local outcome

gen winconW13=0
replace winconW13=1 if ((generalElectionVoteW13==1 & Winner17==1)|(generalElectionVoteW13==2 & Winner17==2)|(generalElectionVoteW13==3 & Winner17==3)|(generalElectionVoteW13==4 & Winner17==4)|(generalElectionVoteW13==5 & Winner17==5)|(generalElectionVoteW13==7 & Winner17==7))
replace winconW13=. if generalElectionVoteW13==.
label values winconW13 win_label
tab winconW13 

save bes_2017_temp.dta, replace

**# 2c. Coding affective polarization

use bes_2017_temp.dta, clear

recode likeConW13 likeConW10 likeLabW13 likeLabW10 likeLDW13 likeLDW10 likeUKIPW13 likeUKIPW10 likeGrnW13 likeGrnW10 likeSNPW10 likeSNPW13 likePCW10 likePCW13 (9999=.)

recode likeSNPW10 likeSNPW13 likePCW10 likePCW13 (0/10=.) if countryW13==1
recode likeSNPW10 likeSNPW13 (0/10=.) if countryW13==3

*** Without listwise deletion

* First - code dummies to indicate whether the R provides pre- and post-election like scores for each party 

bysort id: gen con_nonmiss=1 if likeConW10!=.&likeConW13!=.
bysort id: gen lab_nonmiss=1 if likeLabW10!=.&likeLabW13!=.
bysort id: gen ld_nonmiss=1 if likeLDW10!=.&likeLDW13!=.
bysort id: gen ukip_nonmiss=1 if likeUKIPW10!=.&likeUKIPW13!=.
bysort id: gen green_nonmiss=1 if likeGrnW10!=.&likeGrnW13!=.
bysort id: gen snp_nonmiss=1 if likeSNPW10!=.&likeSNPW13!=.
bysort id: gen pc_nonmiss=1 if likePCW10!=.&likePCW13!=.

* then, identify the number of parties that each individual provide like scores for at both waves

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding like score n at W10

summ ageW10
summ likeConW10

tab wave10

egen likescore_n_eng=rownonmiss(likeConW10 likeLabW10 likeLDW10 likeUKIPW10 likeGrnW10) if countryW13==1
egen likescore_n_sco=rownonmiss(likeConW10 likeLabW10 likeLDW10 likeUKIPW10 likeGrnW10 likeSNPW10) if countryW13==2
egen likescore_n_wal=rownonmiss(likeConW10 likeLabW10 likeLDW10 likeUKIPW10 likeGrnW10 likePCW10) if countryW13==3

gen likescore_n_W10=likescore_n_eng if countryW13==1
replace likescore_n_W10=likescore_n_sco if countryW13==2
replace likescore_n_W10=likescore_n_wal if countryW13==3

tab likescore_n_W10

** Code at W13
* do this next

drop likescore_n_eng likescore_n_sco likescore_n_wal

egen likescore_n_eng=rownonmiss(likeConW13 likeLabW13 likeLDW13 likeUKIPW13 likeGrnW13) if countryW13==1
egen likescore_n_sco=rownonmiss(likeConW13 likeLabW13 likeLDW13 likeUKIPW13 likeGrnW13 likeSNPW13) if countryW13==2
egen likescore_n_wal=rownonmiss(likeConW13 likeLabW13 likeLDW13 likeUKIPW13 likeGrnW13 likePCW13) if countryW13==3

gen likescore_n_W13=likescore_n_eng if countryW13==1
replace likescore_n_W13=likescore_n_sco if countryW13==2
replace likescore_n_W13=likescore_n_wal if countryW13==3

tab likescore_n_W13

drop likescore_n_eng likescore_n_sco likescore_n_wal

* First question - how many parties do people provide like scores for at W10 and W13? Focus on Eng to compare with mean_ldl

tab likescore_n_W10 if countryW13==1
tab likescore_n_W13 if countryW13==1


* The mean_ldl vars correspond to those with a full set of like scores

*** Calculating the AP scores without listwise at W13

* First, calculate the weighted like score for each party, with country-specific weights

gen vsw_likecon=0.454*likeConW13 if countryW13==1
replace vsw_likecon=0.286*likeConW13 if countryW13==2
replace vsw_likecon=0.336*likeConW13 if countryW13==3

gen vsw_likelab=0.419*likeLabW13 if countryW13==1
replace vsw_likelab=0.271*likeLabW13 if countryW13==2
replace vsw_likelab=0.489*likeLabW13 if countryW13==3

gen vsw_likeld=0.078*likeLDW13 if countryW13==1
replace vsw_likeld=0.068*likeLDW13 if countryW13==2
replace vsw_likeld=0.045*likeLDW13 if countryW13==3

gen vsw_likeukip=0.021*likeUKIPW13 if countryW13==1
replace vsw_likeukip=0.002*likeUKIPW13 if countryW13==2
replace vsw_likeukip=0.02*likeUKIPW13 if countryW13==3

gen vsw_likegrn=0.019*likeGrnW13 if countryW13==1
replace vsw_likegrn=0.002*likeGrnW13 if countryW13==2
replace vsw_likegrn=0.003*likeGrnW13 if countryW13==3

gen vsw_likesnp=0.369*likeSNPW13 if countryW13==2

gen vsw_likepc=0.104*likePCW13 if countryW13==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc


* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W13 if mean_ldl_all!=.

** Computing AP scores

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.454*((likeConW13 - mean_ldl_all)^2) if countryW13==1
replace ap_con=0.286*((likeConW13 - mean_ldl_all)^2) if countryW13==2
replace ap_con=0.336*((likeConW13 - mean_ldl_all)^2) if countryW13==3

gen ap_lab=0.419*((likeLabW13 - mean_ldl_all)^2) if countryW13==1
replace ap_lab=0.271*((likeLabW13 - mean_ldl_all)^2) if countryW13==2
replace ap_lab=0.489*((likeLabW13 - mean_ldl_all)^2) if countryW13==3

gen ap_ld=0.078*((likeLDW13 - mean_ldl_all)^2) if countryW13==1
replace ap_ld=0.068*((likeLDW13 - mean_ldl_all)^2) if countryW13==2
replace ap_ld=0.045*((likeLDW13 - mean_ldl_all)^2) if countryW13==3

gen ap_ukip=0.021*((likeUKIPW13 - mean_ldl_all)^2) if countryW13==1
replace ap_ukip=0.002*((likeUKIPW13 - mean_ldl_all)^2) if countryW13==2
replace ap_ukip=0.02*((likeUKIPW13 - mean_ldl_all)^2) if countryW13==3

gen ap_grn=0.019*((likeGrnW13 - mean_ldl_all)^2) if countryW13==1
replace ap_grn=0.002*((likeGrnW13 - mean_ldl_all)^2) if countryW13==2
replace ap_grn=0.003*((likeGrnW13 - mean_ldl_all)^2) if countryW13==3

gen ap_snp=0.369*((likeSNPW13 - mean_ldl_all)^2) if countryW13==2

gen ap_pc=0.104*((likePCW13 - mean_ldl_all)^2) if countryW13==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W13=sqrt(affpol_all)

summ affpol_nlw_W13

* Then - again - tab this with the likescore_n_w6 variable make sure the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W13 if affpol_nlw_W13!=.

*** Coding at wave 10

* First, calculate the weighted like score for each party, with country-specific weights

drop mean_ldl_all

gen vsw_likecon=0.454*likeConW10 if countryW13==1
replace vsw_likecon=0.286*likeConW10 if countryW13==2
replace vsw_likecon=0.336*likeConW10 if countryW13==3

gen vsw_likelab=0.419*likeLabW10 if countryW13==1
replace vsw_likelab=0.271*likeLabW10 if countryW13==2
replace vsw_likelab=0.489*likeLabW10 if countryW13==3

gen vsw_likeld=0.078*likeLDW10 if countryW13==1
replace vsw_likeld=0.068*likeLDW10 if countryW13==2
replace vsw_likeld=0.045*likeLDW10 if countryW13==3

gen vsw_likeukip=0.021*likeUKIPW10 if countryW13==1
replace vsw_likeukip=0.002*likeUKIPW10 if countryW13==2
replace vsw_likeukip=0.02*likeUKIPW10 if countryW13==3

gen vsw_likegrn=0.019*likeGrnW10 if countryW13==1
replace vsw_likegrn=0.002*likeGrnW10 if countryW13==2
replace vsw_likegrn=0.003*likeGrnW10 if countryW13==3

gen vsw_likesnp=0.369*likeSNPW10 if countryW13==2

gen vsw_likepc=0.104*likePCW10 if countryW13==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc

* Then compare this with the mean_ldl obs across the three previous country-specific vars 

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W10 if mean_ldl_all!=.

* Then I can compute the affective polarization score 

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.454*((likeConW10 - mean_ldl_all)^2) if countryW13==1
replace ap_con=0.286*((likeConW10 - mean_ldl_all)^2) if countryW13==2
replace ap_con=0.336*((likeConW10 - mean_ldl_all)^2) if countryW13==3

gen ap_lab=0.419*((likeLabW10 - mean_ldl_all)^2) if countryW13==1
replace ap_lab=0.271*((likeLabW10 - mean_ldl_all)^2) if countryW13==2
replace ap_lab=0.489*((likeLabW10 - mean_ldl_all)^2) if countryW13==3

gen ap_ld=0.078*((likeLDW10 - mean_ldl_all)^2) if countryW13==1
replace ap_ld=0.068*((likeLDW10 - mean_ldl_all)^2) if countryW13==2
replace ap_ld=0.045*((likeLDW10 - mean_ldl_all)^2) if countryW13==3

gen ap_ukip=0.021*((likeUKIPW10 - mean_ldl_all)^2) if countryW13==1
replace ap_ukip=0.002*((likeUKIPW10 - mean_ldl_all)^2) if countryW13==2
replace ap_ukip=0.02*((likeUKIPW10 - mean_ldl_all)^2) if countryW13==3

gen ap_grn=0.019*((likeGrnW10 - mean_ldl_all)^2) if countryW13==1
replace ap_grn=0.002*((likeGrnW10 - mean_ldl_all)^2) if countryW13==2
replace ap_grn=0.003*((likeGrnW10 - mean_ldl_all)^2) if countryW13==3

gen ap_snp=0.369*((likeSNPW10 - mean_ldl_all)^2) if countryW13==2

gen ap_pc=0.104*((likePCW10 - mean_ldl_all)^2) if countryW13==3

* Next, add the AP scores together

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W10=sqrt(affpol_all)

summ affpol_nlw_W10

* Then - again - tab this with the likescore_n variable to see if the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W10 if affpol_nlw_W10!=.

* Code a copy of the W13 variable for use with the W11 measure. 

gen affpol_proximate_W13=affpol_nlw_W13

* And now, the final step is to only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

gen affpol_nlw_ch_W13=affpol_nlw_W13-affpol_nlw_W10

replace affpol_nlw_ch_W13=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W10=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W13=. if bw_likescore_n==0|bw_likescore_n==1

summ affpol_nlw_ch_W13 // 19,579 obs

tab bw_likescore_n if affpol_nlw_ch_W13!=.

save bes_2017_temp_v2.dta, replace

**# 2d. Coding IP like and OP dislike 

use bes_2017_temp_v2.dta, clear

** In-party like 
* Identifying each R's most-liked party 

* Wave 10 

sort id 
egen IP_like_MLP_W10=rowmax(likeConW10 likeLabW10 likeLDW10 likeUKIPW10 likeGrnW10 likeSNPW10 likePCW10)

summ IP_like_MLP_W10

** Wave 13. 

sort id 
egen IP_like_PE_W13=rowmax(likeConW13 likeLabW13 likeLDW13 likeUKIPW13 likeGrnW13 likeSNPW13 likePCW13)

summ IP_like_PE_W13

* Coding the change score 

gen IP_like_ch_W13=IP_like_PE_W13-IP_like_MLP_W10 

summ IP_like_ch_W13

summ IP_like_MLP_W10
summ IP_like_PE_W13
summ IP_like_ch_W13

* Create copy of W13 var for coding more proximate measure 

gen IP_like_proximate_W13=IP_like_PE_W13

* Again retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

replace IP_like_MLP_W10=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_PE_W13=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ch_W13=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_MLP_W10
summ IP_like_PE_W13
summ IP_like_ch_W13

tab likescore_n_W13 if IP_like_ch_W13!=.

** Out-party dislike - single party. 

* Closest party in consituency (if one won in constituency), party one lost to in constituency (if one lost).

* Wave 10. If they lost: 

gen OP_dislike_CON_W10 = .

local partylike likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W10 = `var' if winconW13 == 0 & Winner17 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W10 = `var' if winconW13 == 1 & Second17 == `i'
}

summ OP_dislike_CON_W10

* Wave 13. If they lost: 

gen OP_dislike_CON_W13 = .

local partylike likeConW13 likeLabW13 likeLDW13 likeSNPW13 likePCW13 likeUKIPW13 likeGrnW13

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W13 = `var' if winconW13 == 0 & Winner17 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W13 = `var' if winconW13 == 1 & Second17 == `i'
}

summ OP_dislike_CON_W13

* reverse coding the measure

gen OP_dislike_CON_R_W10=(0-OP_dislike_CON_W10)+10
summ OP_dislike_CON_R_W10
gen OP_dislike_S_PE_W13=(0-OP_dislike_CON_W13)+10
summ OP_dislike_S_PE_W13

* Coding change score 

gen OP_dislike_S_ch_W13=OP_dislike_S_PE_W13-OP_dislike_CON_R_W10

summ OP_dislike_S_ch_W13

tab likescore_n_W13 if OP_dislike_S_ch_W13!=.

** Out-party dislike - multi-party 

* This is a measure of feeling toward all parties, other than the most-liked party. 

* Wave 10

egen total_like=rowtotal(likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10)

egen total_parties=rownonmiss(likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10)

bysort id: gen OP_dislike_multi_W10=(total_like-IP_like_MLP_W10)/(total_parties-1)

summ OP_dislike_multi_W10

drop total_like total_parties

*Wave 13:

egen total_like=rowtotal(likeConW13 likeLabW13 likeLDW13 likeSNPW13 likePCW13 likeUKIPW13 likeGrnW13)

egen total_parties=rownonmiss(likeConW13 likeLabW13 likeLDW13 likeSNPW13 likePCW13 likeUKIPW13 likeGrnW13)

bysort id: gen OP_dislike_multi_W13=(total_like-IP_like_proximate_W13)/(total_parties-1)

summ OP_dislike_multi_W13

drop total_like total_parties

* Finally, we reverse code this measure

gen OP_multi_W13=(0-OP_dislike_multi_W13)+10
gen OP_multi_W10=(0-OP_dislike_multi_W10)+10

summ OP_multi_W10
summ OP_multi_W13

gen OP_dislike_M_ch_W13=OP_multi_W13-OP_multi_W10

summ OP_dislike_M_ch_W13

* Create copy of W13 measure for coding more proximate like score 

gen OP_multi_proximate_W13=OP_multi_W13

* Dropping obs without like scores for 2+ parties at both waves

replace OP_multi_W10=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_multi_W13=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ch_W13=. if bw_likescore_n==0|bw_likescore_n==1

tab likescore_n_W13 if OP_dislike_M_ch_W13!=.

**# 2e. Coding alternative AP measures 

*** Alternative measures for PID 

** In-party like 

* Wave 10

tab pid_wsqueeze_w10

gen IP_like_PID_W10=likeConW10 if pid_wsqueeze_w10==1
replace IP_like_PID_W10=likeLabW10 if pid_wsqueeze_w10==2
replace IP_like_PID_W10=likeLDW10 if pid_wsqueeze_w10==3
replace IP_like_PID_W10=likeSNPW10 if pid_wsqueeze_w10==4
replace IP_like_PID_W10=likePCW10 if pid_wsqueeze_w10==5
replace IP_like_PID_W10=likeUKIPW10 if pid_wsqueeze_w10==6
replace IP_like_PID_W10=likeGrnW10 if pid_wsqueeze_w10==7

* Wave 13 

gen IP_like_PID_W13=likeConW13 if pid_wsqueeze_w10==1
replace IP_like_PID_W13=likeLabW13 if pid_wsqueeze_w10==2
replace IP_like_PID_W13=likeLDW13 if pid_wsqueeze_w10==3
replace IP_like_PID_W13=likeSNPW13 if pid_wsqueeze_w10==4
replace IP_like_PID_W13=likePCW13 if pid_wsqueeze_w10==5
replace IP_like_PID_W13=likeUKIPW13 if pid_wsqueeze_w10==6
replace IP_like_PID_W13=likeGrnW13 if pid_wsqueeze_w10==7

* Coding change score 

gen IP_like_PID_ch_W13=IP_like_PID_W13-IP_like_PID_W10
replace IP_like_PID_ch_W13=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

replace IP_like_PID_ch_W13=IP_like_ch_W13 if pid_wsqueeze_w10==10|pid_wsqueeze_w10==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace IP_like_PID_W10=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_PID_W10=IP_like_MLP_W10 if pid_wsqueeze_w10==10|pid_wsqueeze_w10==9999

sum IP_like_PID_W10
sum IP_like_PID_ch_W13 // 19,283

** Out-party dislike multi 

* Wave 3

egen total_like=rowtotal(likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10)

egen total_parties=rownonmiss(likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10)

bysort id: gen OP_dislike_M_PID_W10=(total_like-IP_like_PID_W10)/(total_parties-1)

summ OP_dislike_M_PID_W10

drop total_like total_parties

*Wave 6:

egen total_like=rowtotal(likeConW13 likeLabW13 likeLDW13 likeSNPW13 likePCW13 likeUKIPW13 likeGrnW13)

egen total_parties=rownonmiss(likeConW13 likeLabW13 likeLDW13 likeSNPW13 likePCW13 likeUKIPW13 likeGrnW13)

bysort id: gen OP_dislike_M_PID_W13=(total_like-IP_like_PID_W13)/(total_parties-1)

summ OP_dislike_M_PID_W13

drop total_like total_parties

* Reverse coding 

gen OP_multi_PID_W13=(0-OP_dislike_M_PID_W13)+10
gen OP_multi_PID_W10=(0-OP_dislike_M_PID_W10)+10

sum OP_multi_PID_W13
sum OP_multi_PID_W10

* Coding change score 

gen OP_disike_M_PID_ch_W13=OP_multi_PID_W13-OP_multi_PID_W10
replace OP_disike_M_PID_ch_W13=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

replace OP_disike_M_PID_ch_W13=OP_dislike_M_ch_W13 if pid_wsqueeze_w10==10|pid_wsqueeze_w10==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace OP_multi_PID_W10=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_multi_PID_W10=OP_multi_W10 if pid_wsqueeze_w10==10|pid_wsqueeze_w10==9999

sum OP_multi_PID_W10
sum OP_disike_M_PID_ch_W13 // 15,104 obs

*** Reiljan API index

* Begin by coding respondent PID at wave 10

tab partyIdW13
tab partyIdSqueezeW13

gen API_pid_2017=1 if partyIdW13==1|partyIdSqueezeW13==1
replace API_pid_2017=2 if partyIdW13==2|partyIdSqueezeW13==2
replace API_pid_2017=3 if partyIdW13==3|partyIdSqueezeW13==3
replace API_pid_2017=4 if partyIdW13==4|partyIdSqueezeW13==4
replace API_pid_2017=5 if partyIdW13==5|partyIdSqueezeW13==5
replace API_pid_2017=6 if partyIdW13==6|partyIdSqueezeW13==6
replace API_pid_2017=7 if partyIdW13==7|partyIdSqueezeW13==7
replace API_pid_2017=8 if partyIdW13==8|partyIdSqueezeW13==8|partyIdW13==9|partyIdSqueezeW13==9

tab API_pid_2017
label define API_pid_label 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other"
label values API_pid_2017 API_pid_label

// Coding VS scores

gen ConVsENG_2017=0.454
gen LabVsENG_2017=0.419
gen LDVsENG_2017=0.078
gen UKIPVsENG_2017=0.021
gen GrnVsENG_2017=0.019

gen ConVsSCO_2017=0.286
gen LabVsSCO_2017=0.271
gen LDVsSCO_2017=0.068
gen SNPVsSCO_2017=0.369
gen UKIPVsSCO_2017=0.002
gen GrnVsSCO_2017=0.002

gen ConVsWAL_2017=0.336
gen LabVsWAL_2017=0.489
gen LDVsWAL_2017=0.045
gen PCVsWAL_2017=0.104
gen UKIPVsWAL_2017=0.02
gen GrnVsWAL_2017=0.003

// Coding IP like score

gen IP_like_W10=likeConW10 if API_pid_2017==1
replace IP_like_W10=likeLabW10 if API_pid_2017==2
replace IP_like_W10=likeLDW10 if API_pid_2017==3
replace IP_like_W10=likeSNPW10 if API_pid_2017==4
replace IP_like_W10=likePCW10 if API_pid_2017==5
replace IP_like_W10=likeUKIPW10 if API_pid_2017==6
replace IP_like_W10=likeGrnW10 if API_pid_2017==7

gen IP_like_W13=likeConW13 if API_pid_2017==1
replace IP_like_W13=likeLabW13 if API_pid_2017==2
replace IP_like_W13=likeLDW13 if API_pid_2017==3
replace IP_like_W13=likeSNPW13 if API_pid_2017==4
replace IP_like_W13=likePCW13 if API_pid_2017==5
replace IP_like_W13=likeUKIPW13 if API_pid_2017==6
replace IP_like_W13=likeGrnW13 if API_pid_2017==7

// API - Wave 10


*Conservatives:

gen AP_Con_ENG_W10=((IP_like_W10-likeLabW10)*(LabVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W10-likeLDW10)*(LDVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsENG_2017/(1-ConVsENG_2017))) if API_pid_2017==1&countryW10==1

gen AP_Con_SCO_W10=((IP_like_W10-likeLabW10)*(LabVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W10-likeLDW10)*(LDVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W10-likeSNPW10)*(SNPVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsSCO_2017/(1-ConVsSCO_2017))) if API_pid_2017==1&countryW10==2

gen AP_Con_WAL_W10=((IP_like_W10-likeLabW10)*(LabVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W10-likeLDW10)*(LDVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W10-likePCW10)*(PCVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsWAL_2017/(1-ConVsWAL_2017))) if API_pid_2017==1&countryW10==3

*Labour:

gen AP_Lab_ENG_W10=((IP_like_W10-likeConW10)*(ConVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W10-likeLDW10)*(LDVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsENG_2017/(1-LabVsENG_2017))) if API_pid_2017==2&countryW10==1

gen AP_Lab_SCO_W10=((IP_like_W10-likeConW10)*(ConVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W10-likeLDW10)*(LDVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W10-likeSNPW10)*(SNPVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsSCO_2017/(1-LabVsSCO_2017))) if API_pid_2017==2&countryW10==2

gen AP_Lab_WAL_W10=((IP_like_W10-likeConW10)*(ConVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W10-likeLDW10)*(LDVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W10-likePCW10)*(PCVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsWAL_2017/(1-LabVsWAL_2017))) if API_pid_2017==2&countryW10==3

*Lib Dems:

gen AP_LD_ENG_W10=((IP_like_W10-likeConW10)*(ConVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W10-likeLabW10)*(LabVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsENG_2017/(1-LDVsENG_2017))) if API_pid_2017==3&countryW10==1

gen AP_LD_SCO_W10=((IP_like_W10-likeConW10)*(ConVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W10-likeLabW10)*(LabVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W10-likeSNPW10)*(SNPVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsSCO_2017/(1-LDVsSCO_2017))) if API_pid_2017==3&countryW10==2

gen AP_LD_WAL_W10=((IP_like_W10-likeConW10)*(ConVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W10-likeLabW10)*(LabVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W10-likePCW10)*(PCVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsWAL_2017/(1-LDVsWAL_2017))) if API_pid_2017==3&countryW10==3

*SNP:

gen AP_SNP_W10=((IP_like_W10-likeConW10)*(ConVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W10-likeLabW10)*(LabVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W10-likeLDW10)*(LDVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsSCO_2017/(1-SNPVsSCO_2017))) if API_pid_2017==4&countryW10==2
tab AP_SNP_W10

*PC:

gen AP_PC_W10=((IP_like_W10-likeConW10)*(ConVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W10-likeLabW10)*(LabVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W10-likeLDW10)*(LDVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsWAL_2017/(1-PCVsWAL_2017))) if API_pid_2017==5&countryW10==3
tab AP_PC_W10

*UKIP:

gen AP_UKIP_ENG_W10=((IP_like_W10-likeConW10)*(ConVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W10-likeLabW10)*(LabVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W10-likeLDW10)*(LDVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsENG_2017/(1-UKIPVsENG_2017))) if API_pid_2017==6&countryW10==1

gen AP_UKIP_SCO_W10=((IP_like_W10-likeConW10)*(ConVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W10-likeLabW10)*(LabVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W10-likeSNPW10)*(SNPVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W10-likeLDW10)*(LDVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsSCO_2017/(1-UKIPVsSCO_2017))) if API_pid_2017==6&countryW10==2

gen AP_UKIP_WAL_W10=((IP_like_W10-likeConW10)*(ConVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W10-likeLabW10)*(LabVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W10-likePCW10)*(PCVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W10-likeLDW10)*(LDVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W10-likeGrnW10)*(GrnVsWAL_2017/(1-UKIPVsWAL_2017))) if API_pid_2017==6&countryW10==3

*Greens:

gen AP_Grn_ENG_W10=((IP_like_W10-likeConW10)*(ConVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W10-likeLabW10)*(LabVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W10-likeLDW10)*(LDVsENG_2017/(1-GrnVsENG_2017))) if API_pid_2017==7&countryW10==1

gen AP_Grn_SCO_W10=((IP_like_W10-likeConW10)*(ConVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W10-likeLabW10)*(LabVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W10-likeSNPW10)*(SNPVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W10-likeLDW10)*(LDVsSCO_2017/(1-GrnVsSCO_2017))) if API_pid_2017==7&countryW10==2

gen AP_Grn_WAL_W10=((IP_like_W10-likeConW10)*(ConVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W10-likeLabW10)*(LabVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W10-likePCW10)*(PCVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W10-likeUKIPW10)*(UKIPVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W10-likeLDW10)*(LDVsWAL_2017/(1-GrnVsWAL_2017))) if API_pid_2017==7&countryW10==3

*AP: 

gen AP_R_W10=.
replace AP_R_W10=AP_Con_ENG_W10 if countryW10==1&API_pid_2017==1
replace AP_R_W10=AP_Con_SCO_W10 if countryW10==2&API_pid_2017==1
replace AP_R_W10=AP_Con_WAL_W10 if countryW10==3&API_pid_2017==1
replace AP_R_W10=AP_Lab_ENG_W10 if countryW10==1&API_pid_2017==2
replace AP_R_W10=AP_Lab_SCO_W10 if countryW10==2&API_pid_2017==2
replace AP_R_W10=AP_Lab_WAL_W10 if countryW10==3&API_pid_2017==2
replace AP_R_W10=AP_LD_ENG_W10 if countryW10==1&API_pid_2017==3
replace AP_R_W10=AP_LD_SCO_W10 if countryW10==2&API_pid_2017==3
replace AP_R_W10=AP_LD_WAL_W10 if countryW10==3&API_pid_2017==3
replace AP_R_W10=AP_SNP_W10 if API_pid_2017==4
replace AP_R_W10=AP_PC_W10 if API_pid_2017==5
replace AP_R_W10=AP_UKIP_ENG_W10 if countryW10==1&API_pid_2017==6
replace AP_R_W10=AP_UKIP_SCO_W10 if countryW10==2&API_pid_2017==6
replace AP_R_W10=AP_UKIP_WAL_W10 if countryW10==3&API_pid_2017==6
replace AP_R_W10=AP_Grn_ENG_W10 if countryW10==1&API_pid_2017==7
replace AP_R_W10=AP_Grn_SCO_W10 if countryW10==2&API_pid_2017==7
replace AP_R_W10=AP_Grn_WAL_W10 if countryW10==3&API_pid_2017==7

summ AP_R_W10

// AP - Wave 13

*Conservatives:

gen AP_Con_ENG_W13=((IP_like_W13-likeLabW13)*(LabVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W13-likeLDW13)*(LDVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsENG_2017/(1-ConVsENG_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsENG_2017/(1-ConVsENG_2017))) if API_pid_2017==1&countryW13==1

gen AP_Con_SCO_W13=((IP_like_W13-likeLabW13)*(LabVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W13-likeLDW13)*(LDVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W13-likeSNPW13)*(SNPVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsSCO_2017/(1-ConVsSCO_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsSCO_2017/(1-ConVsSCO_2017))) if API_pid_2017==1&countryW13==2

gen AP_Con_WAL_W13=((IP_like_W13-likeLabW13)*(LabVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W13-likeLDW13)*(LDVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W13-likePCW13)*(PCVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsWAL_2017/(1-ConVsWAL_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsWAL_2017/(1-ConVsWAL_2017))) if API_pid_2017==1&countryW13==3

*Labour:

gen AP_Lab_ENG_W13=((IP_like_W13-likeConW13)*(ConVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W13-likeLDW13)*(LDVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsENG_2017/(1-LabVsENG_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsENG_2017/(1-LabVsENG_2017))) if API_pid_2017==2&countryW13==1

gen AP_Lab_SCO_W13=((IP_like_W13-likeConW13)*(ConVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W13-likeLDW13)*(LDVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W13-likeSNPW13)*(SNPVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsSCO_2017/(1-LabVsSCO_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsSCO_2017/(1-LabVsSCO_2017))) if API_pid_2017==2&countryW13==2

gen AP_Lab_WAL_W13=((IP_like_W13-likeConW13)*(ConVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W13-likeLDW13)*(LDVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W13-likePCW13)*(PCVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsWAL_2017/(1-LabVsWAL_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsWAL_2017/(1-LabVsWAL_2017))) if API_pid_2017==2&countryW13==3

*Lib Dems:

gen AP_LD_ENG_W13=((IP_like_W13-likeConW13)*(ConVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W13-likeLabW13)*(LabVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsENG_2017/(1-LDVsENG_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsENG_2017/(1-LDVsENG_2017))) if API_pid_2017==3&countryW13==1

gen AP_LD_SCO_W13=((IP_like_W13-likeConW13)*(ConVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W13-likeLabW13)*(LabVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W13-likeSNPW13)*(SNPVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsSCO_2017/(1-LDVsSCO_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsSCO_2017/(1-LDVsSCO_2017))) if API_pid_2017==3&countryW13==2

gen AP_LD_WAL_W13=((IP_like_W13-likeConW13)*(ConVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W13-likeLabW13)*(LabVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W13-likePCW13)*(PCVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsWAL_2017/(1-LDVsWAL_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsWAL_2017/(1-LDVsWAL_2017))) if API_pid_2017==3&countryW13==3

*SNP:

gen AP_SNP_W13=((IP_like_W13-likeConW13)*(ConVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W13-likeLabW13)*(LabVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W13-likeLDW13)*(LDVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsSCO_2017/(1-SNPVsSCO_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsSCO_2017/(1-SNPVsSCO_2017))) if API_pid_2017==4&countryW13==2
tab AP_SNP_W13

*PC:

gen AP_PC_W13=((IP_like_W13-likeConW13)*(ConVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W13-likeLabW13)*(LabVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W13-likeLDW13)*(LDVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsWAL_2017/(1-PCVsWAL_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsWAL_2017/(1-PCVsWAL_2017))) if API_pid_2017==5&countryW13==3
tab AP_PC_W13

*UKIP:

gen AP_UKIP_ENG_W13=((IP_like_W13-likeConW13)*(ConVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W13-likeLabW13)*(LabVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W13-likeLDW13)*(LDVsENG_2017/(1-UKIPVsENG_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsENG_2017/(1-UKIPVsENG_2017))) if API_pid_2017==6&countryW13==1

gen AP_UKIP_SCO_W13=((IP_like_W13-likeConW13)*(ConVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W13-likeLabW13)*(LabVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W13-likeSNPW13)*(SNPVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W13-likeLDW13)*(LDVsSCO_2017/(1-UKIPVsSCO_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsSCO_2017/(1-UKIPVsSCO_2017))) if API_pid_2017==6&countryW13==2

gen AP_UKIP_WAL_W13=((IP_like_W13-likeConW13)*(ConVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W13-likeLabW13)*(LabVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W13-likePCW13)*(PCVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W13-likeLDW13)*(LDVsWAL_2017/(1-UKIPVsWAL_2017)))+((IP_like_W13-likeGrnW13)*(GrnVsWAL_2017/(1-UKIPVsWAL_2017))) if API_pid_2017==6&countryW13==3

*Greens:

gen AP_Grn_ENG_W13=((IP_like_W13-likeConW13)*(ConVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W13-likeLabW13)*(LabVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsENG_2017/(1-GrnVsENG_2017)))+((IP_like_W13-likeLDW13)*(LDVsENG_2017/(1-GrnVsENG_2017))) if API_pid_2017==7&countryW13==1

gen AP_Grn_SCO_W13=((IP_like_W13-likeConW13)*(ConVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W13-likeLabW13)*(LabVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W13-likeSNPW13)*(SNPVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsSCO_2017/(1-GrnVsSCO_2017)))+((IP_like_W13-likeLDW13)*(LDVsSCO_2017/(1-GrnVsSCO_2017))) if API_pid_2017==7&countryW13==2

gen AP_Grn_WAL_W13=((IP_like_W13-likeConW13)*(ConVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W13-likeLabW13)*(LabVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W13-likePCW13)*(PCVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W13-likeUKIPW13)*(UKIPVsWAL_2017/(1-GrnVsWAL_2017)))+((IP_like_W13-likeLDW13)*(LDVsWAL_2017/(1-GrnVsWAL_2017))) if API_pid_2017==7&countryW13==3

*AP: 

gen AP_R_W13=.
replace AP_R_W13=AP_Con_ENG_W13 if countryW13==1&API_pid_2017==1
replace AP_R_W13=AP_Con_SCO_W13 if countryW13==2&API_pid_2017==1
replace AP_R_W13=AP_Con_WAL_W13 if countryW13==3&API_pid_2017==1
replace AP_R_W13=AP_Lab_ENG_W13 if countryW13==1&API_pid_2017==2
replace AP_R_W13=AP_Lab_SCO_W13 if countryW13==2&API_pid_2017==2
replace AP_R_W13=AP_Lab_WAL_W13 if countryW13==3&API_pid_2017==2
replace AP_R_W13=AP_LD_ENG_W13 if countryW13==1&API_pid_2017==3
replace AP_R_W13=AP_LD_SCO_W13 if countryW13==2&API_pid_2017==3
replace AP_R_W13=AP_LD_WAL_W13 if countryW13==3&API_pid_2017==3
replace AP_R_W13=AP_SNP_W13 if API_pid_2017==4
replace AP_R_W13=AP_PC_W13 if API_pid_2017==5
replace AP_R_W13=AP_UKIP_ENG_W13 if countryW13==1&API_pid_2017==6
replace AP_R_W13=AP_UKIP_SCO_W13 if countryW13==2&API_pid_2017==6
replace AP_R_W13=AP_UKIP_WAL_W13 if countryW13==3&API_pid_2017==6
replace AP_R_W13=AP_Grn_ENG_W13 if countryW13==1&API_pid_2017==7
replace AP_R_W13=AP_Grn_SCO_W13 if countryW13==2&API_pid_2017==7
replace AP_R_W13=AP_Grn_WAL_W13 if countryW13==3&API_pid_2017==7

summ AP_R_W13

* Mean scores 

summ AP_R_W10
summ AP_R_W13

* coding change score 

gen AP_R_ch_W13=AP_R_W13-AP_R_W10

*** Wagner measure 

* Wave 10 

egen MLP_W10=rowmax(likeConW10 likeLabW10 likeLDW10 likeUKIPW10 likeGrnW10 likeSNPW10 likePCW10)

gen AP_distance_ENG_W10=sqrt((ConVsENG_2017*((likeConW10-MLP_W10)^2))+(LabVsENG_2017*((likeLabW10-MLP_W10)^2))+(LDVsENG_2017*((likeLDW10-MLP_W10)^2))+(UKIPVsENG_2017*((likeUKIPW10-MLP_W10)^2))+(GrnVsENG_2017*((likeGrnW10-MLP_W10)^2))) if countryW10==1
gen AP_distance_SCO_W10=sqrt((ConVsSCO_2017*((likeConW10-MLP_W10)^2))+(LabVsSCO_2017*((likeLabW10-MLP_W10)^2))+(LDVsSCO_2017*((likeLDW10-MLP_W10)^2))+(UKIPVsSCO_2017*((likeUKIPW10-MLP_W10)^2))+(GrnVsSCO_2017*((likeGrnW10-MLP_W10)^2))+(SNPVsSCO_2017*((likeSNPW10-MLP_W10)^2))) if countryW10==2
gen AP_distance_WAL_W10=sqrt((ConVsWAL_2017*((likeConW10-MLP_W10)^2))+(LabVsWAL_2017*((likeLabW10-MLP_W10)^2))+(LDVsWAL_2017*((likeLDW10-MLP_W10)^2))+(UKIPVsWAL_2017*((likeUKIPW10-MLP_W10)^2))+(GrnVsWAL_2017*((likeGrnW10-MLP_W10)^2))+(PCVsWAL_2017*((likePCW10-MLP_W10)^2))) if countryW10==3

gen AP_distance_W10=.
replace AP_distance_W10=AP_distance_ENG_W10 if countryW10==1
replace AP_distance_W10=AP_distance_SCO_W10 if countryW10==2
replace AP_distance_W10=AP_distance_WAL_W10 if countryW10==3
summ AP_distance_W10

*Wave 13: 

egen MLP_W13=rowmax(likeConW13 likeLabW13 likeLDW13 likeUKIPW13 likeGrnW13 likeSNPW13 likePCW13)

gen AP_distance_ENG_W13=sqrt((ConVsENG_2017*((likeConW13-MLP_W13)^2))+(LabVsENG_2017*((likeLabW13-MLP_W13)^2))+(LDVsENG_2017*((likeLDW13-MLP_W13)^2))+(UKIPVsENG_2017*((likeUKIPW13-MLP_W13)^2))+(GrnVsENG_2017*((likeGrnW13-MLP_W13)^2))) if countryW13==1
gen AP_distance_SCO_W13=sqrt((ConVsSCO_2017*((likeConW13-MLP_W13)^2))+(LabVsSCO_2017*((likeLabW13-MLP_W13)^2))+(LDVsSCO_2017*((likeLDW13-MLP_W13)^2))+(UKIPVsSCO_2017*((likeUKIPW13-MLP_W13)^2))+(GrnVsSCO_2017*((likeGrnW13-MLP_W13)^2))+(SNPVsSCO_2017*((likeSNPW13-MLP_W13)^2))) if countryW13==2
gen AP_distance_WAL_W13=sqrt((ConVsWAL_2017*((likeConW13-MLP_W13)^2))+(LabVsWAL_2017*((likeLabW13-MLP_W13)^2))+(LDVsWAL_2017*((likeLDW13-MLP_W13)^2))+(UKIPVsWAL_2017*((likeUKIPW13-MLP_W13)^2))+(GrnVsWAL_2017*((likeGrnW13-MLP_W13)^2))+(PCVsWAL_2017*((likePCW13-MLP_W13)^2))) if countryW13==3

gen AP_distance_W13=.
replace AP_distance_W13=AP_distance_ENG_W13 if countryW13==1
replace AP_distance_W13=AP_distance_SCO_W13 if countryW13==2
replace AP_distance_W13=AP_distance_WAL_W13 if countryW13==3
summ AP_distance_W13

gen AP_distance_ch_W13=AP_distance_W13-AP_distance_W10

summ AP_distance_ch_W13

save bes_2017_temp_v3.dta, replace

**# 2f. Coding wave 11 measures 

use bes_2017_temp_v3.dta, clear

** Affective polarization

* recoding missing values for party like-dislike

recode likeConW11 likeLabW11 likeLDW11 likeUKIPW11 likeGrnW11 likeSNPW11 likePCW11 (9999=.)

recode likeSNPW11 likePCW11 (0/10=.) if countryW13==1
recode likeSNPW11 (0/10=.) if countryW13==3

*** Without listwise deletion

drop con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss

bysort id: gen con_nonmiss=1 if likeConW11!=.&likeConW13!=.
bysort id: gen lab_nonmiss=1 if likeLabW11!=.&likeLabW13!=.
bysort id: gen ld_nonmiss=1 if likeLDW11!=.&likeLDW13!=.
bysort id: gen ukip_nonmiss=1 if likeUKIPW11!=.&likeUKIPW13!=.
bysort id: gen green_nonmiss=1 if likeGrnW11!=.&likeGrnW13!=.
bysort id: gen snp_nonmiss=1 if likeSNPW11!=.&likeSNPW13!=.
bysort id: gen pc_nonmiss=1 if likePCW11!=.&likePCW13!=.

* then, identify the number of parties that each individual provide like scores for at both waves

drop bw_likescore_n

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss ukip_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding like score n at W11

egen likescore_n_eng=rownonmiss(likeConW11 likeLabW11 likeLDW11 likeUKIPW11 likeGrnW11) if countryW13==1
egen likescore_n_sco=rownonmiss(likeConW11 likeLabW11 likeLDW11 likeUKIPW11 likeGrnW11 likeSNPW11) if countryW13==2
egen likescore_n_wal=rownonmiss(likeConW11 likeLabW11 likeLDW11 likeUKIPW11 likeGrnW11 likePCW11) if countryW13==3

gen likescore_n_W11=likescore_n_eng if countryW13==1
replace likescore_n_W11=likescore_n_sco if countryW13==2
replace likescore_n_W11=likescore_n_wal if countryW13==3

tab likescore_n_W11

* how many parties do people provide like scores for at W11 and W13? Focus on Eng to compare with mean_ldl

tab likescore_n_W11 if countryW13==1
tab likescore_n_W13 if countryW13==1


* The mean_ldl vars correspond to those with a full set of like scores

*** Calculating the AP scores without listwise at W11

gen vsw_likecon=0.454*likeConW11 if countryW13==1
replace vsw_likecon=0.286*likeConW11 if countryW13==2
replace vsw_likecon=0.336*likeConW11 if countryW13==3

gen vsw_likelab=0.419*likeLabW11 if countryW13==1
replace vsw_likelab=0.271*likeLabW11 if countryW13==2
replace vsw_likelab=0.489*likeLabW11 if countryW13==3

gen vsw_likeld=0.078*likeLDW11 if countryW13==1
replace vsw_likeld=0.068*likeLDW11 if countryW13==2
replace vsw_likeld=0.045*likeLDW11 if countryW13==3

gen vsw_likeukip=0.021*likeUKIPW11 if countryW13==1
replace vsw_likeukip=0.002*likeUKIPW11 if countryW13==2
replace vsw_likeukip=0.02*likeUKIPW11 if countryW13==3

gen vsw_likegrn=0.019*likeGrnW11 if countryW13==1
replace vsw_likegrn=0.002*likeGrnW11 if countryW13==2
replace vsw_likegrn=0.003*likeGrnW11 if countryW13==3

gen vsw_likesnp=0.369*likeSNPW11 if countryW13==2

gen vsw_likepc=0.104*likePCW11 if countryW13==3

* Then, calculate the mean by summing these scores up 

drop mean_ldl_all 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likeukip vsw_likegrn vsw_likesnp vsw_likepc)

tab likescore_n_W11 if mean_ldl_all!=.

** Computing AP scores

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.454*((likeConW11 - mean_ldl_all)^2) if countryW13==1
replace ap_con=0.286*((likeConW11 - mean_ldl_all)^2) if countryW13==2
replace ap_con=0.336*((likeConW11 - mean_ldl_all)^2) if countryW13==3

gen ap_lab=0.419*((likeLabW11 - mean_ldl_all)^2) if countryW13==1
replace ap_lab=0.271*((likeLabW11 - mean_ldl_all)^2) if countryW13==2
replace ap_lab=0.489*((likeLabW11 - mean_ldl_all)^2) if countryW13==3

gen ap_ld=0.078*((likeLDW11 - mean_ldl_all)^2) if countryW13==1
replace ap_ld=0.068*((likeLDW11 - mean_ldl_all)^2) if countryW13==2
replace ap_ld=0.045*((likeLDW11 - mean_ldl_all)^2) if countryW13==3

gen ap_ukip=0.021*((likeUKIPW11 - mean_ldl_all)^2) if countryW13==1
replace ap_ukip=0.002*((likeUKIPW11 - mean_ldl_all)^2) if countryW13==2
replace ap_ukip=0.02*((likeUKIPW11 - mean_ldl_all)^2) if countryW13==3

gen ap_grn=0.019*((likeGrnW11 - mean_ldl_all)^2) if countryW13==1
replace ap_grn=0.002*((likeGrnW11 - mean_ldl_all)^2) if countryW13==2
replace ap_grn=0.003*((likeGrnW11 - mean_ldl_all)^2) if countryW13==3

gen ap_snp=0.369*((likeSNPW11 - mean_ldl_all)^2) if countryW13==2

gen ap_pc=0.104*((likePCW11 - mean_ldl_all)^2) if countryW13==3

* Next, add the AP scores together

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_ukip ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W11=sqrt(affpol_all)

summ affpol_nlw_W11

* Then - again - tab this with the likescore_n variable to see if the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W11 if affpol_nlw_W11!=.

* Coding the change score - using the copy of the W13 variable 

gen affpol_nlw_ich_W13=affpol_proximate_W13-affpol_nlw_W11

* And now, the final step is to only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

replace affpol_nlw_ich_W13=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W11=. if bw_likescore_n==0|bw_likescore_n==1

* Then - compare this to the original variable 

summ affpol_nlw_ich_W13 // 22,952 obs - 3373 more than less proximate measure
summ affpol_nlw_ch_W13

disp 22952-19579

tab bw_likescore_n if affpol_nlw_ich_W13!=.

** In-party like

sort id 
egen IP_like_MLP_W11=rowmax(likeConW11 likeLabW11 likeLDW11 likeUKIPW11 likeGrnW11 likeSNPW11 likePCW11)

* Coding the change score

gen IP_like_ich_W13=IP_like_proximate_W13-IP_like_MLP_W11

replace IP_like_MLP_W11=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ich_W13=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_ich_W13 // 22,952 obs (same as affpol)
summ IP_like_ch_W13

** Out-party dislike
* Single
* If they lost: 

gen OP_dislike_CON_W11 = .

local partylike likeConW11 likeLabW11 likeLDW11 likeSNPW11 likePCW11 likeUKIPW11 likeGrnW11

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W11 = `var' if winconW13 == 0 & Winner17 == `i'
}

*If they won:

forvalues i = 1/7 {
    local var : word `i' of `partylike'
    replace OP_dislike_CON_W11 = `var' if winconW13 == 1 & Second17 == `i'
}

summ OP_dislike_CON_W11

gen OP_dislike_CON_R_W11=(0-OP_dislike_CON_W11)+10
summ OP_dislike_CON_R_W11

gen OP_dislike_S_ich_W13=OP_dislike_S_PE_W13-OP_dislike_CON_R_W11

summ OP_dislike_S_ich_W13 // 21,116 obs
summ OP_dislike_S_ch_W13

disp 21116-17905 // 3211 more than the less proximate measure 

** Out-party dislike - multi-party 

egen total_like=rowtotal(likeConW11 likeLabW11 likeLDW11 likeSNPW11 likePCW11 likeUKIPW11 likeGrnW11)

egen total_parties=rownonmiss(likeConW11 likeLabW11 likeLDW11 likeSNPW11 likePCW11 likeUKIPW11 likeGrnW11)

bysort id: gen OP_dislike_multi_W11=(total_like-IP_like_MLP_W11)/(total_parties-1)

summ OP_dislike_multi_W11

drop total_like total_parties

* Finally, we reverse code this measure

gen OP_multi_W11=(0-OP_dislike_multi_W11)+10

summ OP_multi_W11

gen OP_dislike_M_ich_W13=OP_multi_proximate_W13-OP_multi_W11

replace OP_multi_W11=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ich_W13=. if bw_likescore_n==0|bw_likescore_n==1

summ OP_dislike_M_ich_W13 // 22,952 obs (same as affpol)


**# 2g. Merge with campaign spending data 

* first, save the data 

save bes_2017_temp_v4.dta, replace

use bes_2017_temp_v4.dta, clear

* Coding Constituency var for matching

decode pconW13, gen(Constituency)

drop _merge 

* Complete merge

merge m:1 Constituency using campaign_spending_2017.dta

* Complete match from the master data (100%)

keep if _merge==3

** Adjusting spending scores by electorate size

gen all_party_spend_W13=A_con_spend_W13/Electorate17

summ A_con_spend_W13
summ all_party_spend_W13

replace Conservative_spend_W13=Conservative_spend_W13/Electorate17
replace Labour_spend_W13=Labour_spend_W13/Electorate17
replace LD_spend_W13=LD_spend_W13/Electorate17
replace SNP_spend_W13=SNP_spend_W13/Electorate17
replace PC_spend_W13=PC_spend_W13/Electorate17
replace UKIP_spend_W13=UKIP_spend_W13/Electorate17
replace Green_spend_W13=Green_spend_W13/Electorate17

summ Conservative_spend_W13

save bes_2017_temp_v5.dta, replace

** Calculating in-party spending

use bes_2017_temp_v5.dta, clear 

* first, identify the identity of the in-party - R's most liked party

* first thing I need to do is identify those individuals who have multiple parties with the same max like score


gen match_count_W10 = ///
    cond(!missing(MLP_W10) & MLP_W10 == likeConW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likeLabW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likeLDW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likeGrnW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likeUKIPW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likeSNPW10, 1, 0) + ///
    cond(!missing(MLP_W10) & MLP_W10 == likePCW10, 1, 0)
	
tab match_count_W10

* Now i can code an in-party identifier, using the same numeric structure as for other variables. Con=1; Lab=2; LD=3; SNP=4; PC = 5; UKIP = 6; Green = 7; Other = 8; Brexit= 9.

* First, generate the variable
gen IP_id = .

* Then define the party variables and corresponding ID values
local parties likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10
local ids 1 2 3 4 5 6 7

* Coding a loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    
    replace IP_id = `id' if MLP_W10 == `var' & match_count_W10 == 1
}

* for those individuals with multiple parties with the same max like score, I use PID to differentiate. 

tab partyIdW10
tab partyIdSqueezeW10 if partyIdW10>8

replace IP_id=partyIdW10 if match_count_W10>1&partyIdW10<8
replace IP_id=partyIdSqueezeW10 if match_count_W10>1&partyIdW10>8&partyIdSqueezeW10<8

* This is not a perfect solution - there are still some individuals without a in-party, becuase they have no PID. 

tab match_count_W10 if match_count_W10>1&IP_id==.

* 826 at this wave 

tab IP_id

** Coding the IP spend var

* generating the variable 

gen IP_spend_W13=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W13 Labour_spend_W13 LD_spend_W13 SNP_spend_W13 PC_spend_W13 UKIP_spend_W13 Green_spend_W13
local ids 1 2 3 4 5 6 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_W13 = `var' if IP_id == `id'
}

summ IP_spend_W13

** Coding out-party spend 

bysort id: gen OP_spend_W13=all_party_spend_W13-IP_spend_W13

summ OP_spend_W13

*** PID Spending measures 

* First, code a new in-party ID var that is the in-party for those with a PID; and the MLP for those without a PID. 

tab pid_wsqueeze_w10

gen IP_PID_id=pid_wsqueeze_w10

* Then define the party variables and corresponding ID values
local parties likeConW10 likeLabW10 likeLDW10 likeSNPW10 likePCW10 likeUKIPW10 likeGrnW10
local ids 1 2 3 4 5 6 7

* Coding a loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    
    replace IP_PID_id = `id' if MLP_W10 == `var' & (pid_wsqueeze_w10==10|pid_wsqueeze_w10==9999)
}

tab IP_PID_id

** Coding IP spend. First, generate the variable 

gen IP_spend_PID_W13=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W13 Labour_spend_W13 LD_spend_W13 SNP_spend_W13 PC_spend_W13 UKIP_spend_W13 Green_spend_W13
local ids 1 2 3 4 5 6 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_PID_W13 = `var' if IP_PID_id == `id'
}

summ IP_spend_PID_W13
summ IP_spend_W13

** Coding out-party spend 

bysort id: gen OP_spend_PID_W13=all_party_spend_W13-IP_spend_PID_W13

summ OP_spend_W13
summ OP_spend_PID_W13

**# 2h. Coding party contact data 

tab IP_id

tab partyContact1W13

tab partyContactConW13
tab partyContactLabW13
tab partyContactLDW13
tab partyContactSNPW13
tab partyContactPCW13
tab partyContactUKIPW13
tab partyContactGrnW13
 
recode partyContactConW13 partyContactLabW13 partyContactLDW13 partyContactSNPW13 partyContactPCW13 partyContactUKIPW13 partyContactGrnW13 (9999=.)

* Coding in-party contact 

gen IP_contact_W13=0 if partyContact1W13==0
replace IP_contact_W13=1 if partyContactConW13==1&IP_id==1
replace IP_contact_W13=1 if partyContactLabW13==1&IP_id==2
replace IP_contact_W13=1 if partyContactLDW13==1&IP_id==3
replace IP_contact_W13=1 if partyContactSNPW13==1&IP_id==4
replace IP_contact_W13=1 if partyContactPCW13==1&IP_id==5
replace IP_contact_W13=1 if IP_id==6&partyContactUKIPW13==1
replace IP_contact_W13=1 if IP_id==7&partyContactGrnW13==1

tab IP_contact_W13

* Coding out-party contact 
* Going to use the self-reported contact variables for specific out-parties to code this, rather than inferring from the in-party variable, because I think it is important that the respondent specifically reports out-party contact

egen OP_contact_W13=rowtotal(partyContactLabW13 partyContactLDW13 partyContactSNPW13 partyContactPCW13 partyContactUKIPW13 partyContactGrnW13) if IP_id==1
ereplace OP_contact_W13=rowtotal(partyContactConW13 partyContactLDW13 partyContactSNPW13 partyContactPCW13 partyContactUKIPW13 partyContactGrnW13) if IP_id==2
ereplace OP_contact_W13=rowtotal(partyContactConW13 partyContactLabW13 partyContactSNPW13 partyContactPCW13 partyContactUKIPW13 partyContactGrnW13) if IP_id==3
ereplace OP_contact_W13=rowtotal(partyContactConW13 partyContactLabW13 partyContactLDW13 partyContactPCW13 partyContactUKIPW13 partyContactGrnW13) if IP_id==4
ereplace OP_contact_W13=rowtotal(partyContactConW13 partyContactLabW13 partyContactLDW13 partyContactUKIPW13 partyContactGrnW13) if IP_id==5
ereplace OP_contact_W13=rowtotal(partyContactLabW13 partyContactLDW13 partyContactSNPW13 partyContactPCW13 partyContactGrnW13) if IP_id==6
ereplace OP_contact_W13=rowtotal(partyContactConW13 partyContactLabW13 partyContactLDW13 partyContactSNPW13 partyContactPCW13 partyContactUKIPW13) if IP_id==7

tab OP_contact_W13
tab IP_contact_W13
tab IP_id

pwcorr IP_contact_W13 OP_contact_W13

* Tactical voting indicator 

tab generalElectionVoteW13

tab pid_wsqueeze_w10

gen tactical_voteW13=1 if (generalElectionVoteW13!=pid_wsqueeze_w10) & (generalElectionVoteW13<10&pid_wsqueeze_w10<10)
replace tactical_voteW13=0 if (generalElectionVoteW13==pid_wsqueeze_w10) & (generalElectionVoteW13<10&pid_wsqueeze_w10<10)

tab tactical_voteW13


**# 2i. Saving the data and preparing for merge

keep affpol* IP_* OP_* countryW13 genderW13 ethnicW13 ageW13 higheducW13 pid_strength_W13 pid_pre_W13 pid_pre_wsqueeze_W13 wingovW13 winconW13 CI_17 panoW13 eff_parties17 raw_margin_W13 pconW13 id AP_R_* AP_distance* IP_spend_W13 OP_spend_W13 R_con_spend_2017 A_con_spend_W13 all_party_spend_W13 farright_ep_W13 Constituency tactical_voteW13

gen affpol_nlw_PE_W13=affpol_nlw_W13

save bes_2017_temp_v6.dta, replace

**# 3. 2019 - BESIP data coding

use BES2024_W29_Panel_v29.1.dta, clear

**# 3a. merge the individual-level data with the competitiveness data

merge m:1 panoW19 using CI_2019_v2.dta

drop if CI_19==.


save bes_2019_temp.dta, replace

**# 3b. coding individual-level variables 

*** PID as a moderator

* Binary measure - does the respondent have a PID?

tab partyIdW16

recode partyIdW16 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_W19)

tab pid_pre_W19

* With the squeeze follow-up

gen pid_wsqueeze_w16=partyIdW16
replace pid_wsqueeze_w16=partyIdSqueezeW16 if partyIdW16==10|partyIdW16==9999
tab pid_wsqueeze_w16

recode pid_wsqueeze_w16 (1/9 11 12=1) (10 9999 = 0), gen(pid_pre_wsqueeze_W19)
tab pid_pre_wsqueeze_W19

* Categorical measure - strength of PID 

tab partyIdStrengthW16

recode partyIdStrengthW16 (1=4 "Very strong") (2=3 "Fairly Strong") (3=2 "Not very strong") (9999=1 "No PID/Don't know"), gen(pid_strength_W19)
replace pid_strength_W19=1 if pid_pre_W19==0

tab pid_strength_W19

*** Controls 

** Education

recode p_edlevelW19 (4/5=1 "Degree") (0/3=0 "No degree"), gen (higheducW19)

** Age

summ ageW19

** Ethnicity 

recode p_ethnicityW19 (1=1 "White British") (else=0 "Other"), gen (ethnicW19)

** country 

tab countryW19

** Gender 

gen genderW19=.
replace genderW19=0 if gender==1 & wave19==1
replace genderW19 = 1 if gender==2 & wave19==1

** National election outcome

tab generalElectionVoteW19
recode generalElectionVoteW19 (1=1) (2/13=0) (9999=.), gen(wingovW19)
tab wingovW19

label define win_label 0 "Loss" 1 "Win"
label values wingovW19 win_label
tab wingovW19

** Local outcome

tab generalElectionVoteW19

gen winconW19=0
replace winconW19=1 if ((generalElectionVoteW19==1 & Winner19==1)|(generalElectionVoteW19==2 & Winner19==2)|(generalElectionVoteW19==3 & Winner19==3)|(generalElectionVoteW19==4 & Winner19==4)|(generalElectionVoteW19==5 & Winner19==5)|(generalElectionVoteW19==7 & Winner19==7))
replace winconW19=. if generalElectionVoteW19==.
label values winconW19 win_label
tab winconW19

**# 3c. Coding affective polarization

* Recoding missing values 

recode likeConW19 likeConW16 likeLabW19 likeLabW16 likeLDW19 likeLDW16 likeBrexitPartyW19 likeBrexitPartyW16 likeGrnW19 likeGrnW16 likeSNPW16 likeSNPW19 likePCW16 likePCW19 (9999=.)

recode likeSNPW16 likeSNPW19 likePCW16 likePCW19 (0/10=.) if countryW19==1
recode likeSNPW16 likeSNPW19 (0/10=.) if countryW19==3

*** Without listwise deletion

bysort id: gen con_nonmiss=1 if likeConW16!=.&likeConW19!=.
bysort id: gen lab_nonmiss=1 if likeLabW16!=.&likeLabW19!=.
bysort id: gen ld_nonmiss=1 if likeLDW16!=.&likeLDW19!=.
bysort id: gen brexit_nonmiss=1 if likeBrexitPartyW16!=.&likeBrexitPartyW19!=.
bysort id: gen green_nonmiss=1 if likeGrnW16!=.&likeGrnW19!=.
bysort id: gen snp_nonmiss=1 if likeSNPW16!=.&likeSNPW19!=.
bysort id: gen pc_nonmiss=1 if likePCW16!=.&likePCW19!=.

* then, identify the number of parties that each individual provide like scores for at both waves

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss brexit_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding like score n at W16

egen likescore_n_eng=rownonmiss(likeConW16 likeLabW16 likeLDW16 likeBrexitPartyW16 likeGrnW16) if countryW19==1
egen likescore_n_sco=rownonmiss(likeConW16 likeLabW16 likeLDW16 likeBrexitPartyW16 likeGrnW16 likeSNPW16) if countryW19==2
egen likescore_n_wal=rownonmiss(likeConW16 likeLabW16 likeLDW16 likeBrexitPartyW16 likeGrnW16 likePCW16) if countryW19==3

gen likescore_n_W16=likescore_n_eng if countryW19==1
replace likescore_n_W16=likescore_n_sco if countryW19==2
replace likescore_n_W16=likescore_n_wal if countryW19==3

tab likescore_n_W16

* Coding like score n at W19

drop likescore_n_eng likescore_n_sco likescore_n_wal

egen likescore_n_eng=rownonmiss(likeConW19 likeLabW19 likeLDW19 likeBrexitPartyW19 likeGrnW19) if countryW19==1
egen likescore_n_sco=rownonmiss(likeConW19 likeLabW19 likeLDW19 likeBrexitPartyW19 likeGrnW19 likeSNPW19) if countryW19==2
egen likescore_n_wal=rownonmiss(likeConW19 likeLabW19 likeLDW19 likeBrexitPartyW19 likeGrnW19 likePCW19) if countryW19==3

gen likescore_n_W19=likescore_n_eng if countryW19==1
replace likescore_n_W19=likescore_n_sco if countryW19==2
replace likescore_n_W19=likescore_n_wal if countryW19==3

tab likescore_n_W19

drop likescore_n_eng likescore_n_sco likescore_n_wal

* how many parties do people provide like scores for at W16 and W19? Focus on Eng to compare with mean_ldl

tab likescore_n_W16 if countryW19==1
tab likescore_n_W19 if countryW19==1


*** Calculating the AP scores without listwise at W19

* First, calculate the weighted like score for each party, with country-specific weights

gen vsw_likecon=0.472*likeConW19 if countryW19==1
replace vsw_likecon=0.251*likeConW19 if countryW19==2
replace vsw_likecon=0.361*likeConW19 if countryW19==3

gen vsw_likelab=0.34*likeLabW19 if countryW19==1
replace vsw_likelab=0.186*likeLabW19 if countryW19==2
replace vsw_likelab=0.409*likeLabW19 if countryW19==3

gen vsw_likeld=0.124*likeLDW19 if countryW19==1
replace vsw_likeld=0.095*likeLDW19 if countryW19==2
replace vsw_likeld=0.06*likeLDW19 if countryW19==3

gen vsw_likebrexit=0.02*likeBrexitPartyW19 if countryW19==1
replace vsw_likebrexit=0.005*likeBrexitPartyW19 if countryW19==2
replace vsw_likebrexit=0.054*likeBrexitPartyW19 if countryW19==3

gen vsw_likegrn=0.03*likeGrnW19 if countryW19==1
replace vsw_likegrn=0.01*likeGrnW19 if countryW19==2
replace vsw_likegrn=0.01*likeGrnW19 if countryW19==3

gen vsw_likesnp=0.45*likeSNPW19 if countryW19==2

gen vsw_likepc=0.099*likePCW19 if countryW19==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W19 if mean_ldl_all!=.

** Computing AP scores

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.472*((likeConW19 - mean_ldl_all)^2) if countryW19==1
replace ap_con=0.251*((likeConW19 - mean_ldl_all)^2) if countryW19==2
replace ap_con=0.361*((likeConW19 - mean_ldl_all)^2) if countryW19==3

gen ap_lab=0.34*((likeLabW19 - mean_ldl_all)^2) if countryW19==1
replace ap_lab=0.186*((likeLabW19 - mean_ldl_all)^2) if countryW19==2
replace ap_lab=0.409*((likeLabW19 - mean_ldl_all)^2) if countryW19==3

gen ap_ld=0.124*((likeLDW19 - mean_ldl_all)^2) if countryW19==1
replace ap_ld=0.095*((likeLDW19 - mean_ldl_all)^2) if countryW19==2
replace ap_ld=0.06*((likeLDW19 - mean_ldl_all)^2) if countryW19==3

gen ap_brexit=0.02*((likeBrexitPartyW19 - mean_ldl_all)^2) if countryW19==1
replace ap_brexit=0.005*((likeBrexitPartyW19 - mean_ldl_all)^2) if countryW19==2
replace ap_brexit=0.054*((likeBrexitPartyW19 - mean_ldl_all)^2) if countryW19==3

gen ap_grn=0.03*((likeGrnW19 - mean_ldl_all)^2) if countryW19==1
replace ap_grn=0.01*((likeGrnW19 - mean_ldl_all)^2) if countryW19==2
replace ap_grn=0.01*((likeGrnW19 - mean_ldl_all)^2) if countryW19==3

gen ap_snp=0.45*((likeSNPW19 - mean_ldl_all)^2) if countryW19==2

gen ap_pc=0.099*((likePCW19 - mean_ldl_all)^2) if countryW19==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W19=sqrt(affpol_all)

summ affpol_nlw_W19

* Then - again - tab this with the likescore_n variable make sure the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W19 if affpol_nlw_W19!=.

*** Coding at wave 16

* First, calculate the weighted like score for each party, with country-specific weights

drop mean_ldl_all

gen vsw_likecon=0.472*likeConW16 if countryW19==1
replace vsw_likecon=0.251*likeConW16 if countryW19==2
replace vsw_likecon=0.361*likeConW16 if countryW19==3

gen vsw_likelab=0.34*likeLabW16 if countryW19==1
replace vsw_likelab=0.186*likeLabW16 if countryW19==2
replace vsw_likelab=0.409*likeLabW16 if countryW19==3

gen vsw_likeld=0.124*likeLDW16 if countryW19==1
replace vsw_likeld=0.095*likeLDW16 if countryW19==2
replace vsw_likeld=0.06*likeLDW16 if countryW19==3

gen vsw_likebrexit=0.02*likeBrexitPartyW16 if countryW19==1
replace vsw_likebrexit=0.005*likeBrexitPartyW16 if countryW19==2
replace vsw_likebrexit=0.054*likeBrexitPartyW16 if countryW19==3

gen vsw_likegrn=0.03*likeGrnW16 if countryW19==1
replace vsw_likegrn=0.01*likeGrnW16 if countryW19==2
replace vsw_likegrn=0.01*likeGrnW16 if countryW19==3

gen vsw_likesnp=0.45*likeSNPW16 if countryW19==2

gen vsw_likepc=0.099*likePCW16 if countryW19==3

* Then, calculate the mean by summing these scores up 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W16 if mean_ldl_all!=.

* Then I can compute the affective polarization score 

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.472*((likeConW16 - mean_ldl_all)^2) if countryW19==1
replace ap_con=0.251*((likeConW16 - mean_ldl_all)^2) if countryW19==2
replace ap_con=0.361*((likeConW16 - mean_ldl_all)^2) if countryW19==3

gen ap_lab=0.34*((likeLabW16 - mean_ldl_all)^2) if countryW19==1
replace ap_lab=0.186*((likeLabW16 - mean_ldl_all)^2) if countryW19==2
replace ap_lab=0.409*((likeLabW16 - mean_ldl_all)^2) if countryW19==3

gen ap_ld=0.124*((likeLDW16 - mean_ldl_all)^2) if countryW19==1
replace ap_ld=0.095*((likeLDW16 - mean_ldl_all)^2) if countryW19==2
replace ap_ld=0.06*((likeLDW16 - mean_ldl_all)^2) if countryW19==3

gen ap_brexit=0.02*((likeBrexitPartyW16 - mean_ldl_all)^2) if countryW19==1
replace ap_brexit=0.005*((likeBrexitPartyW16 - mean_ldl_all)^2) if countryW19==2
replace ap_brexit=0.054*((likeBrexitPartyW16 - mean_ldl_all)^2) if countryW19==3

gen ap_grn=0.03*((likeGrnW16 - mean_ldl_all)^2) if countryW19==1
replace ap_grn=0.01*((likeGrnW16 - mean_ldl_all)^2) if countryW19==2
replace ap_grn=0.01*((likeGrnW16 - mean_ldl_all)^2) if countryW19==3

gen ap_snp=0.45*((likeSNPW16 - mean_ldl_all)^2) if countryW19==2

gen ap_pc=0.099*((likePCW16 - mean_ldl_all)^2) if countryW19==3

* Next, add the AP scores together

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W16=sqrt(affpol_all)

summ affpol_nlw_W16

* Then - again - tab this with the likescore_n variable to see if the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W16 if affpol_nlw_W16!=.

* Code a copy of the W19 variable for use with the W17 measure. 

gen affpol_proximate_W19=affpol_nlw_W19

* And now, the final step is to only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

gen affpol_nlw_ch_W19=affpol_nlw_W19-affpol_nlw_W16

replace affpol_nlw_ch_W19=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W19=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W16=. if bw_likescore_n==0|bw_likescore_n==1

* Then - compare this to the original variable 

summ affpol_nlw_ch_W19 // 20,345 obs

tab bw_likescore_n if affpol_nlw_ch_W19!=.

save bes_2019_temp_v2.dta, replace

**# 3d. Coding IP like and OP dislike 

use bes_2019_temp_v2.dta, clear

** In-party like 
* Identifying each R's most-liked party 

** Wave 16.

egen IP_like_MLP_W16=rowmax(likeConW16 likeLabW16 likeLDW16 likeBrexitPartyW16 likeGrnW16 likeSNPW16 likePCW16)

summ IP_like_MLP_W16

** Wave 19 

egen IP_like_PE_W19=rowmax(likeConW19 likeLabW19 likeLDW19 likeBrexitPartyW19 likeGrnW19 likeSNPW19 likePCW19)

summ IP_like_PE_W19

* Coding change score 

gen IP_like_ch_W19=IP_like_PE_W19-IP_like_MLP_W16

summ IP_like_MLP_W16
summ IP_like_PE_W19
summ IP_like_ch_W19

* Create W19 var for coding more proximate measure 

gen IP_like_proximate_W19=IP_like_PE_W19

* Again retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

replace IP_like_MLP_W16=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_PE_W19=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ch_W19=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_MLP_W16
summ IP_like_PE_W19
summ IP_like_ch_W19

** Out-party dislike - single party. 

* Closest party in consituency (if one won in constituency), party one lost to in constituency (if one lost).

*Wave 16. If they lost:

gen OP_dislike_CON_W16 = .

local winners 1 2 3 4 5 9 7
local likevars likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W16 = `var' if winconW19 == 0 & Winner19 == `win'
}

*If they won:

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W16 = `var' if winconW19 == 1 & Second19 == `win'
}

summ OP_dislike_CON_W16

*Wave 19. If they lost:

gen OP_dislike_CON_W19 = .

local winners 1 2 3 4 5 9 7
local likevars likeConW19 likeLabW19 likeLDW19 likeSNPW19 likePCW19 likeBrexitPartyW19 likeGrnW19

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W19 = `var' if winconW19 == 0 & Winner19 == `win'
}

*If they won:

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W19 = `var' if winconW19 == 1 & Second19 == `win'
}

summ OP_dislike_CON_W19

* Reverse coding these measures 

gen OP_dislike_CON_R_W16=(0-OP_dislike_CON_W16)+10
summ OP_dislike_CON_R_W16

gen OP_dislike_S_PE_W19=(0-OP_dislike_CON_W19)+10
summ OP_dislike_S_PE_W19

gen OP_dislike_S_ch_W19=OP_dislike_S_PE_W19-OP_dislike_CON_R_W16

summ OP_dislike_S_ch_W19

** Out-party dislike - multi-party 

* This is a measure of feeling toward all parties, other than the most-liked party. 

* Wave 16

egen total_like=rowtotal(likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16)

egen total_parties=rownonmiss(likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16)

bysort id: gen OP_dislike_multi_W16=(total_like-IP_like_MLP_W16)/(total_parties-1)

summ OP_dislike_multi_W16

drop total_like total_parties

* Wave 19

egen total_like=rowtotal(likeConW19 likeLabW19 likeLDW19 likeSNPW19 likePCW19 likeBrexitPartyW19 likeGrnW19)

egen total_parties=rownonmiss(likeConW19 likeLabW19 likeLDW19 likeSNPW19 likePCW19 likeBrexitPartyW19 likeGrnW19)

bysort id: gen OP_dislike_multi_W19=(total_like-IP_like_proximate_W19)/(total_parties-1)

summ OP_dislike_multi_W19

drop total_like total_parties

* Finally, we reverse code this measure

gen OP_dislike_M_PE_W19=(0-OP_dislike_multi_W19)+10
gen OP_multi_W16=(0-OP_dislike_multi_W16)+10

summ OP_multi_W16
summ OP_dislike_M_PE_W19

* Create copy of W19 measure for coding more proximate like score 

gen OP_multi_proximate_W19=OP_dislike_M_PE_W19

summ OP_multi_proximate_W19

gen OP_dislike_M_ch_W19=OP_dislike_M_PE_W19-OP_multi_W16

summ OP_dislike_M_ch_W19

replace OP_multi_W16=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_PE_W19=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ch_W19=. if bw_likescore_n==0|bw_likescore_n==1

tab likescore_n_W19 if OP_dislike_M_ch_W19!=.

**# 3e. Coding alternative AP measures 

*** Alternative measures for PID 

** In-party like 

* Wave 16

tab pid_wsqueeze_w16

gen IP_like_PID_W16=likeConW16 if pid_wsqueeze_w16==1
replace IP_like_PID_W16=likeLabW16 if pid_wsqueeze_w16==2
replace IP_like_PID_W16=likeLDW16 if pid_wsqueeze_w16==3
replace IP_like_PID_W16=likeSNPW16 if pid_wsqueeze_w16==4
replace IP_like_PID_W16=likePCW16 if pid_wsqueeze_w16==5
replace IP_like_PID_W16=likeBrexitPartyW16 if pid_wsqueeze_w16==12
replace IP_like_PID_W16=likeGrnW16 if pid_wsqueeze_w16==7

* Wave 19 

gen IP_like_PID_W19=likeConW19 if pid_wsqueeze_w16==1
replace IP_like_PID_W19=likeLabW19 if pid_wsqueeze_w16==2
replace IP_like_PID_W19=likeLDW19 if pid_wsqueeze_w16==3
replace IP_like_PID_W19=likeSNPW19 if pid_wsqueeze_w16==4
replace IP_like_PID_W19=likePCW19 if pid_wsqueeze_w16==5
replace IP_like_PID_W19=likeBrexitPartyW19 if pid_wsqueeze_w16==12
replace IP_like_PID_W19=likeGrnW19 if pid_wsqueeze_w16==7

* Coding change score 

gen IP_like_PID_ch_W19=IP_like_PID_W19-IP_like_PID_W16
replace IP_like_PID_ch_W19=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

replace IP_like_PID_ch_W19=IP_like_ch_W19 if pid_wsqueeze_w16==10|pid_wsqueeze_w16==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace IP_like_PID_W16=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_PID_W16=IP_like_MLP_W16 if pid_wsqueeze_w16==10|pid_wsqueeze_w16==9999

sum IP_like_PID_W16
sum IP_like_PID_ch_W19 // 19,664 obs

** Out-party dislike multi 

* Wave 16

egen total_like=rowtotal(likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16)

egen total_parties=rownonmiss(likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16)

bysort id: gen OP_dislike_M_PID_W16=(total_like-IP_like_PID_W16)/(total_parties-1)

summ OP_dislike_M_PID_W16

drop total_like total_parties

*Wave 19:

egen total_like=rowtotal(likeConW19 likeLabW19 likeLDW19 likeSNPW19 likePCW19 likeBrexitPartyW19 likeGrnW19)

egen total_parties=rownonmiss(likeConW19 likeLabW19 likeLDW19 likeSNPW19 likePCW19 likeBrexitPartyW19 likeGrnW19)

bysort id: gen OP_dislike_M_PID_W19=(total_like-IP_like_PID_W19)/(total_parties-1)

summ OP_dislike_M_PID_W19

drop total_like total_parties

* Reverse coding 

gen OP_multi_PID_W19=(0-OP_dislike_M_PID_W19)+10
gen OP_multi_PID_W16=(0-OP_dislike_M_PID_W16)+10

sum OP_multi_PID_W19
sum OP_multi_PID_W16

* Coding change score 

gen OP_disike_M_PID_ch_W19=OP_multi_PID_W19-OP_multi_PID_W16
replace OP_disike_M_PID_ch_W19=. if bw_likescore_n==0|bw_likescore_n==1

* Replacing change score with the original score for those who do not have a pid 

replace OP_disike_M_PID_ch_W19=OP_dislike_M_ch_W19 if pid_wsqueeze_w16==10|pid_wsqueeze_w16==9999

* Cleaning the lag score - (1) retain only those individuals who provide like scores for at least two parties at both the pre-election and post-election waves; (2) replace lag score with the original score for those who do not have a pid 

replace OP_multi_PID_W16=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_multi_PID_W16=OP_multi_W16 if pid_wsqueeze_w16==10|pid_wsqueeze_w16==9999

sum OP_multi_PID_W16
sum OP_disike_M_PID_ch_W19 // 19,664 obs 

** Reiljan API index

tab partyIdW19
tab partyIdSqueezeW19

gen API_pid_2019=1 if partyIdW19==1|partyIdSqueezeW19==1
replace API_pid_2019=2 if partyIdW19==2|partyIdSqueezeW19==2
replace API_pid_2019=3 if partyIdW19==3|partyIdSqueezeW19==3
replace API_pid_2019=4 if partyIdW19==4|partyIdSqueezeW19==4
replace API_pid_2019=5 if partyIdW19==5|partyIdSqueezeW19==5
replace API_pid_2019=6 if partyIdW19==6|partyIdSqueezeW19==6
replace API_pid_2019=7 if partyIdW19==7|partyIdSqueezeW19==7
replace API_pid_2019=8 if partyIdW19==8|partyIdSqueezeW19==8|partyIdW19==9|partyIdSqueezeW19==9
replace API_pid_2019=9 if partyIdW19==12|partyIdSqueezeW19==12
tab API_pid_2019

label define API_pid_label_2019 1 "Conservative" 2 "Labour" 3 "Lib Dem" 4 "SNP" 5 "Plaid" 6 "UKIP" 7 "Green" 8 "Other" 9 "Brexit"
label values API_pid_2019 API_pid_label_2019

// Coding VS scores

gen ConVsENG_2019=0.472
gen LabVsENG_2019=0.34
gen LDVsENG_2019=0.124
gen BrexitPartyVsENG_2019=0.02
gen GrnVsENG_2019=0.03

gen ConVsSCO_2019=0.251
gen LabVsSCO_2019=0.186
gen LDVsSCO_2019=0.095
gen SNPVsSCO_2019=0.45
gen BrexitPartyVsSCO_2019=0.005
gen GrnVsSCO_2019=0.01

gen ConVsWAL_2019=0.361
gen LabVsWAL_2019=0.409
gen LDVsWAL_2019=0.06
gen PCVsWAL_2019=0.099
gen BrexitPartyVsWAL_2019=0.054
gen GrnVsWAL_2019=0.01

// Coding IP like score

gen IP_like_W16=likeConW16 if API_pid_2019==1
replace IP_like_W16=likeLabW16 if API_pid_2019==2
replace IP_like_W16=likeLDW16 if API_pid_2019==3
replace IP_like_W16=likeSNPW16 if API_pid_2019==4
replace IP_like_W16=likePCW16 if API_pid_2019==5
replace IP_like_W16=likeBrexitPartyW16 if API_pid_2019==6
replace IP_like_W16=likeGrnW16 if API_pid_2019==7
replace IP_like_W16=likeBrexitPartyW16 if API_pid_2019==9

gen IP_like_W19=likeConW19 if API_pid_2019==1
replace IP_like_W19=likeLabW19 if API_pid_2019==2
replace IP_like_W19=likeLDW19 if API_pid_2019==3
replace IP_like_W19=likeSNPW19 if API_pid_2019==4
replace IP_like_W19=likePCW19 if API_pid_2019==5
replace IP_like_W19=likeGrnW19 if API_pid_2019==7
replace IP_like_W19=likeBrexitPartyW19 if API_pid_2019==9

// AP - Wave 16
*Conservatives:

gen AP_Con_ENG_W16=((IP_like_W16-likeLabW16)*(LabVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W16-likeLDW16)*(LDVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsENG_2019/(1-ConVsENG_2019))) if API_pid_2019==1&countryW16==1

gen AP_Con_SCO_W16=((IP_like_W16-likeLabW16)*(LabVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W16-likeLDW16)*(LDVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W16-likeSNPW16)*(SNPVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsSCO_2019/(1-ConVsSCO_2019))) if API_pid_2019==1&countryW16==2

gen AP_Con_WAL_W16=((IP_like_W16-likeLabW16)*(LabVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W16-likeLDW16)*(LDVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W16-likePCW16)*(PCVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsWAL_2019/(1-ConVsWAL_2019))) if API_pid_2019==1&countryW16==3

*Labour:

gen AP_Lab_ENG_W16=((IP_like_W16-likeConW16)*(ConVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W16-likeLDW16)*(LDVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsENG_2019/(1-LabVsENG_2019))) if API_pid_2019==2&countryW16==1

gen AP_Lab_SCO_W16=((IP_like_W16-likeConW16)*(ConVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W16-likeLDW16)*(LDVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W16-likeSNPW16)*(SNPVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsSCO_2019/(1-LabVsSCO_2019))) if API_pid_2019==2&countryW16==2

gen AP_Lab_WAL_W16=((IP_like_W16-likeConW16)*(ConVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W16-likeLDW16)*(LDVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W16-likePCW16)*(PCVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsWAL_2019/(1-LabVsWAL_2019))) if API_pid_2019==2&countryW16==3

*Lib Dems:

gen AP_LD_ENG_W16=((IP_like_W16-likeConW16)*(ConVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W16-likeLabW16)*(LabVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsENG_2019/(1-LDVsENG_2019))) if API_pid_2019==3&countryW16==1

gen AP_LD_SCO_W16=((IP_like_W16-likeConW16)*(ConVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W16-likeLabW16)*(LabVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W16-likeSNPW16)*(SNPVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsSCO_2019/(1-LDVsSCO_2019))) if API_pid_2019==3&countryW16==2

gen AP_LD_WAL_W16=((IP_like_W16-likeConW16)*(ConVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W16-likeLabW16)*(LabVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W16-likePCW16)*(PCVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsWAL_2019/(1-LDVsWAL_2019))) if API_pid_2019==3&countryW16==3

*SNP:

gen AP_SNP_W16=((IP_like_W16-likeConW16)*(ConVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W16-likeLabW16)*(LabVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W16-likeLDW16)*(LDVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsSCO_2019/(1-SNPVsSCO_2019))) if API_pid_2019==4&countryW16==2
tab AP_SNP_W16

*PC:

gen AP_PC_W16=((IP_like_W16-likeConW16)*(ConVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W16-likeLabW16)*(LabVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W16-likeLDW16)*(LDVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsWAL_2019/(1-PCVsWAL_2019))) if API_pid_2019==5&countryW16==3
tab AP_PC_W16

*BrexitParty:

gen AP_BrexitParty_ENG_W16=((IP_like_W16-likeConW16)*(ConVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W16-likeLabW16)*(LabVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W16-likeLDW16)*(LDVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsENG_2019/(1-BrexitPartyVsENG_2019))) if API_pid_2019==6&countryW16==1

gen AP_BrexitParty_SCO_W16=((IP_like_W16-likeConW16)*(ConVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W16-likeLabW16)*(LabVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W16-likeSNPW16)*(SNPVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W16-likeLDW16)*(LDVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsSCO_2019/(1-BrexitPartyVsSCO_2019))) if API_pid_2019==6&countryW16==2

gen AP_BrexitParty_WAL_W16=((IP_like_W16-likeConW16)*(ConVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W16-likeLabW16)*(LabVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W16-likePCW16)*(PCVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W16-likeLDW16)*(LDVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W16-likeGrnW16)*(GrnVsWAL_2019/(1-BrexitPartyVsWAL_2019))) if API_pid_2019==6&countryW16==3

*Greens:

gen AP_Grn_ENG_W16=((IP_like_W16-likeConW16)*(ConVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W16-likeLabW16)*(LabVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W16-likeLDW16)*(LDVsENG_2019/(1-GrnVsENG_2019))) if API_pid_2019==7&countryW16==1

gen AP_Grn_SCO_W16=((IP_like_W16-likeConW16)*(ConVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W16-likeLabW16)*(LabVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W16-likeSNPW16)*(SNPVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W16-likeLDW16)*(LDVsSCO_2019/(1-GrnVsSCO_2019))) if API_pid_2019==7&countryW16==2

gen AP_Grn_WAL_W16=((IP_like_W16-likeConW16)*(ConVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W16-likeLabW16)*(LabVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W16-likePCW16)*(PCVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W16-likeBrexitPartyW16)*(BrexitPartyVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W16-likeLDW16)*(LDVsWAL_2019/(1-GrnVsWAL_2019))) if API_pid_2019==7&countryW16==3

*AP: 

gen AP_R_W16=.
replace AP_R_W16=AP_Con_ENG_W16 if countryW16==1&API_pid_2019==1
replace AP_R_W16=AP_Con_SCO_W16 if countryW16==2&API_pid_2019==1
replace AP_R_W16=AP_Con_WAL_W16 if countryW16==3&API_pid_2019==1
replace AP_R_W16=AP_Lab_ENG_W16 if countryW16==1&API_pid_2019==2
replace AP_R_W16=AP_Lab_SCO_W16 if countryW16==2&API_pid_2019==2
replace AP_R_W16=AP_Lab_WAL_W16 if countryW16==3&API_pid_2019==2
replace AP_R_W16=AP_LD_ENG_W16 if countryW16==1&API_pid_2019==3
replace AP_R_W16=AP_LD_SCO_W16 if countryW16==2&API_pid_2019==3
replace AP_R_W16=AP_LD_WAL_W16 if countryW16==3&API_pid_2019==3
replace AP_R_W16=AP_SNP_W16 if API_pid_2019==4
replace AP_R_W16=AP_PC_W16 if API_pid_2019==5
replace AP_R_W16=AP_BrexitParty_ENG_W16 if countryW16==1&API_pid_2019==6
replace AP_R_W16=AP_BrexitParty_SCO_W16 if countryW16==2&API_pid_2019==6
replace AP_R_W16=AP_BrexitParty_WAL_W16 if countryW16==3&API_pid_2019==6
replace AP_R_W16=AP_Grn_ENG_W16 if countryW16==1&API_pid_2019==7
replace AP_R_W16=AP_Grn_SCO_W16 if countryW16==2&API_pid_2019==7
replace AP_R_W16=AP_Grn_WAL_W16 if countryW16==3&API_pid_2019==7

summ AP_R_W16


* Wave 19

*Conservatives:

gen AP_Con_ENG_W19=((IP_like_W19-likeLabW19)*(LabVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W19-likeLDW19)*(LDVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsENG_2019/(1-ConVsENG_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsENG_2019/(1-ConVsENG_2019))) if API_pid_2019==1&countryW19==1

gen AP_Con_SCO_W19=((IP_like_W19-likeLabW19)*(LabVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W19-likeLDW19)*(LDVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W19-likeSNPW19)*(SNPVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsSCO_2019/(1-ConVsSCO_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsSCO_2019/(1-ConVsSCO_2019))) if API_pid_2019==1&countryW19==2

gen AP_Con_WAL_W19=((IP_like_W19-likeLabW19)*(LabVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W19-likeLDW19)*(LDVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W19-likePCW19)*(PCVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsWAL_2019/(1-ConVsWAL_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsWAL_2019/(1-ConVsWAL_2019))) if API_pid_2019==1&countryW19==3

*Labour:

gen AP_Lab_ENG_W19=((IP_like_W19-likeConW19)*(ConVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W19-likeLDW19)*(LDVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsENG_2019/(1-LabVsENG_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsENG_2019/(1-LabVsENG_2019))) if API_pid_2019==2&countryW19==1

gen AP_Lab_SCO_W19=((IP_like_W19-likeConW19)*(ConVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W19-likeLDW19)*(LDVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W19-likeSNPW19)*(SNPVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsSCO_2019/(1-LabVsSCO_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsSCO_2019/(1-LabVsSCO_2019))) if API_pid_2019==2&countryW19==2

gen AP_Lab_WAL_W19=((IP_like_W19-likeConW19)*(ConVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W19-likeLDW19)*(LDVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W19-likePCW19)*(PCVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsWAL_2019/(1-LabVsWAL_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsWAL_2019/(1-LabVsWAL_2019))) if API_pid_2019==2&countryW19==3

*Lib Dems:

gen AP_LD_ENG_W19=((IP_like_W19-likeConW19)*(ConVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W19-likeLabW19)*(LabVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsENG_2019/(1-LDVsENG_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsENG_2019/(1-LDVsENG_2019))) if API_pid_2019==3&countryW19==1

gen AP_LD_SCO_W19=((IP_like_W19-likeConW19)*(ConVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W19-likeLabW19)*(LabVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W19-likeSNPW19)*(SNPVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsSCO_2019/(1-LDVsSCO_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsSCO_2019/(1-LDVsSCO_2019))) if API_pid_2019==3&countryW19==2

gen AP_LD_WAL_W19=((IP_like_W19-likeConW19)*(ConVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W19-likeLabW19)*(LabVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W19-likePCW19)*(PCVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsWAL_2019/(1-LDVsWAL_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsWAL_2019/(1-LDVsWAL_2019))) if API_pid_2019==3&countryW19==3

*SNP:

gen AP_SNP_W19=((IP_like_W19-likeConW19)*(ConVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W19-likeLabW19)*(LabVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W19-likeLDW19)*(LDVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsSCO_2019/(1-SNPVsSCO_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsSCO_2019/(1-SNPVsSCO_2019))) if API_pid_2019==4&countryW19==2
tab AP_SNP_W19

*PC:

gen AP_PC_W19=((IP_like_W19-likeConW19)*(ConVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W19-likeLabW19)*(LabVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W19-likeLDW19)*(LDVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsWAL_2019/(1-PCVsWAL_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsWAL_2019/(1-PCVsWAL_2019))) if API_pid_2019==5&countryW19==3
tab AP_PC_W19

*BrexitParty:

gen AP_BrexitParty_ENG_W19=((IP_like_W19-likeConW19)*(ConVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W19-likeLabW19)*(LabVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W19-likeLDW19)*(LDVsENG_2019/(1-BrexitPartyVsENG_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsENG_2019/(1-BrexitPartyVsENG_2019))) if API_pid_2019==6&countryW19==1

gen AP_BrexitParty_SCO_W19=((IP_like_W19-likeConW19)*(ConVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W19-likeLabW19)*(LabVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W19-likeSNPW19)*(SNPVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W19-likeLDW19)*(LDVsSCO_2019/(1-BrexitPartyVsSCO_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsSCO_2019/(1-BrexitPartyVsSCO_2019))) if API_pid_2019==6&countryW19==2

gen AP_BrexitParty_WAL_W19=((IP_like_W19-likeConW19)*(ConVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W19-likeLabW19)*(LabVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W19-likePCW19)*(PCVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W19-likeLDW19)*(LDVsWAL_2019/(1-BrexitPartyVsWAL_2019)))+((IP_like_W19-likeGrnW19)*(GrnVsWAL_2019/(1-BrexitPartyVsWAL_2019))) if API_pid_2019==6&countryW19==3

*Greens:

gen AP_Grn_ENG_W19=((IP_like_W19-likeConW19)*(ConVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W19-likeLabW19)*(LabVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsENG_2019/(1-GrnVsENG_2019)))+((IP_like_W19-likeLDW19)*(LDVsENG_2019/(1-GrnVsENG_2019))) if API_pid_2019==7&countryW19==1

gen AP_Grn_SCO_W19=((IP_like_W19-likeConW19)*(ConVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W19-likeLabW19)*(LabVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W19-likeSNPW19)*(SNPVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsSCO_2019/(1-GrnVsSCO_2019)))+((IP_like_W19-likeLDW19)*(LDVsSCO_2019/(1-GrnVsSCO_2019))) if API_pid_2019==7&countryW19==2

gen AP_Grn_WAL_W19=((IP_like_W19-likeConW19)*(ConVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W19-likeLabW19)*(LabVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W19-likePCW19)*(PCVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W19-likeBrexitPartyW19)*(BrexitPartyVsWAL_2019/(1-GrnVsWAL_2019)))+((IP_like_W19-likeLDW19)*(LDVsWAL_2019/(1-GrnVsWAL_2019))) if API_pid_2019==7&countryW19==3

*AP: 

gen AP_R_W19=.
replace AP_R_W19=AP_Con_ENG_W19 if countryW19==1&API_pid_2019==1
replace AP_R_W19=AP_Con_SCO_W19 if countryW19==2&API_pid_2019==1
replace AP_R_W19=AP_Con_WAL_W19 if countryW19==3&API_pid_2019==1
replace AP_R_W19=AP_Lab_ENG_W19 if countryW19==1&API_pid_2019==2
replace AP_R_W19=AP_Lab_SCO_W19 if countryW19==2&API_pid_2019==2
replace AP_R_W19=AP_Lab_WAL_W19 if countryW19==3&API_pid_2019==2
replace AP_R_W19=AP_LD_ENG_W19 if countryW19==1&API_pid_2019==3
replace AP_R_W19=AP_LD_SCO_W19 if countryW19==2&API_pid_2019==3
replace AP_R_W19=AP_LD_WAL_W19 if countryW19==3&API_pid_2019==3
replace AP_R_W19=AP_SNP_W19 if API_pid_2019==4
replace AP_R_W19=AP_PC_W19 if API_pid_2019==5
replace AP_R_W19=AP_BrexitParty_ENG_W19 if countryW19==1&API_pid_2019==6
replace AP_R_W19=AP_BrexitParty_SCO_W19 if countryW19==2&API_pid_2019==6
replace AP_R_W19=AP_BrexitParty_WAL_W19 if countryW19==3&API_pid_2019==6
replace AP_R_W19=AP_Grn_ENG_W19 if countryW19==1&API_pid_2019==7
replace AP_R_W19=AP_Grn_SCO_W19 if countryW19==2&API_pid_2019==7
replace AP_R_W19=AP_Grn_WAL_W19 if countryW19==3&API_pid_2019==7

summ AP_R_W19
* Coding change score:

gen AP_R_ch_W19=AP_R_W19-AP_R_W16

summ AP_R_ch_W19

*** Wagner measure

* Wave 16

egen MLP_W16=rowmax(likeConW16 likeLabW16 likeLDW16 likeBrexitPartyW16 likeGrnW16 likeSNPW16 likePCW16)

gen AP_distance_ENG_W16=sqrt((ConVsENG_2019*((likeConW16-MLP_W16)^2))+(LabVsENG_2019*((likeLabW16-MLP_W16)^2))+(LDVsENG_2019*((likeLDW16-MLP_W16)^2))+(BrexitPartyVsENG_2019*((likeBrexitPartyW16-MLP_W16)^2))+(GrnVsENG_2019*((likeGrnW16-MLP_W16)^2))) if countryW16==1
gen AP_distance_SCO_W16=sqrt((ConVsSCO_2019*((likeConW16-MLP_W16)^2))+(LabVsSCO_2019*((likeLabW16-MLP_W16)^2))+(LDVsSCO_2019*((likeLDW16-MLP_W16)^2))+(BrexitPartyVsSCO_2019*((likeBrexitPartyW16-MLP_W16)^2))+(GrnVsSCO_2019*((likeGrnW16-MLP_W16)^2))+(SNPVsSCO_2019*((likeSNPW16-MLP_W16)^2))) if countryW16==2
gen AP_distance_WAL_W16=sqrt((ConVsWAL_2019*((likeConW16-MLP_W16)^2))+(LabVsWAL_2019*((likeLabW16-MLP_W16)^2))+(LDVsWAL_2019*((likeLDW16-MLP_W16)^2))+(BrexitPartyVsWAL_2019*((likeBrexitPartyW16-MLP_W16)^2))+(GrnVsWAL_2019*((likeGrnW16-MLP_W16)^2))+(PCVsWAL_2019*((likePCW16-MLP_W16)^2))) if countryW16==3

gen AP_distance_W16=.
replace AP_distance_W16=AP_distance_ENG_W16 if countryW16==1
replace AP_distance_W16=AP_distance_SCO_W16 if countryW16==2
replace AP_distance_W16=AP_distance_WAL_W16 if countryW16==3
summ AP_distance_W16

* Wave 19:

egen MLP_W19=rowmax(likeConW19 likeLabW19 likeLDW19 likeBrexitPartyW19 likeGrnW19 likeSNPW19 likePCW19)

gen AP_distance_ENG_W19=sqrt((ConVsENG_2019*((likeConW19-MLP_W19)^2))+(LabVsENG_2019*((likeLabW19-MLP_W19)^2))+(LDVsENG_2019*((likeLDW19-MLP_W19)^2))+(BrexitPartyVsENG_2019*((likeBrexitPartyW19-MLP_W19)^2))+(GrnVsENG_2019*((likeGrnW19-MLP_W19)^2))) if countryW19==1
gen AP_distance_SCO_W19=sqrt((ConVsSCO_2019*((likeConW19-MLP_W19)^2))+(LabVsSCO_2019*((likeLabW19-MLP_W19)^2))+(LDVsSCO_2019*((likeLDW19-MLP_W19)^2))+(BrexitPartyVsSCO_2019*((likeBrexitPartyW19-MLP_W19)^2))+(GrnVsSCO_2019*((likeGrnW19-MLP_W19)^2))+(SNPVsSCO_2019*((likeSNPW19-MLP_W19)^2))) if countryW19==2
gen AP_distance_WAL_W19=sqrt((ConVsWAL_2019*((likeConW19-MLP_W19)^2))+(LabVsWAL_2019*((likeLabW19-MLP_W19)^2))+(LDVsWAL_2019*((likeLDW19-MLP_W19)^2))+(BrexitPartyVsWAL_2019*((likeBrexitPartyW19-MLP_W19)^2))+(GrnVsWAL_2019*((likeGrnW19-MLP_W19)^2))+(PCVsWAL_2019*((likePCW19-MLP_W19)^2))) if countryW19==3

gen AP_distance_W19=.
replace AP_distance_W19=AP_distance_ENG_W19 if countryW19==1
replace AP_distance_W19=AP_distance_SCO_W19 if countryW19==2
replace AP_distance_W19=AP_distance_WAL_W19 if countryW19==3
summ AP_distance_W19

gen AP_distance_ch_W19=AP_distance_W19-AP_distance_W16

**# 3f. Coding wave 17 measures

** Affective polarization

* recoding missing values for party like-dislike

recode likeConW17 likeLabW17 likeLDW17 likeBrexitPartyW17 likeGrnW17 likeSNPW17 likePCW17 (9999=.)

recode likeSNPW17 likePCW17 (0/10=.) if countryW19==1
recode likeSNPW17 (0/10=.) if countryW19==3

* Coding nonmissing identifiers

drop con_nonmiss lab_nonmiss ld_nonmiss brexit_nonmiss green_nonmiss snp_nonmiss pc_nonmiss

bysort id: gen con_nonmiss=1 if likeConW17!=.&likeConW19!=.
bysort id: gen lab_nonmiss=1 if likeLabW17!=.&likeLabW19!=.
bysort id: gen ld_nonmiss=1 if likeLDW17!=.&likeLDW19!=.
bysort id: gen brexit_nonmiss=1 if likeBrexitPartyW17!=.&likeBrexitPartyW19!=.
bysort id: gen green_nonmiss=1 if likeGrnW17!=.&likeGrnW19!=.
bysort id: gen snp_nonmiss=1 if likeSNPW17!=.&likeSNPW19!=.
bysort id: gen pc_nonmiss=1 if likePCW17!=.&likePCW19!=.

* then, identify the number of parties that each individual provide like scores for at both waves

drop bw_likescore_n

egen bw_likescore_n=rowtotal(con_nonmiss lab_nonmiss ld_nonmiss brexit_nonmiss green_nonmiss snp_nonmiss pc_nonmiss)

* Coding like score n at W16

egen likescore_n_eng=rownonmiss(likeConW17 likeLabW17 likeLDW17 likeBrexitPartyW17 likeGrnW17) if countryW19==1
egen likescore_n_sco=rownonmiss(likeConW17 likeLabW17 likeLDW17 likeBrexitPartyW17 likeGrnW17 likeSNPW17) if countryW19==2
egen likescore_n_wal=rownonmiss(likeConW17 likeLabW17 likeLDW17 likeBrexitPartyW17 likeGrnW17 likePCW17) if countryW19==3

gen likescore_n_W17=likescore_n_eng if countryW19==1
replace likescore_n_W17=likescore_n_sco if countryW19==2
replace likescore_n_W17=likescore_n_wal if countryW19==3

tab likescore_n_W17

* Already have likescore_n at wave 19 

tab likescore_n_W19

*** Calculating the AP scores without listwise at W17

gen vsw_likecon=0.472*likeConW17 if countryW19==1
replace vsw_likecon=0.251*likeConW17 if countryW19==2
replace vsw_likecon=0.361*likeConW17 if countryW19==3

gen vsw_likelab=0.34*likeLabW17 if countryW19==1
replace vsw_likelab=0.186*likeLabW17 if countryW19==2
replace vsw_likelab=0.409*likeLabW17 if countryW19==3

gen vsw_likeld=0.124*likeLDW17 if countryW19==1
replace vsw_likeld=0.095*likeLDW17 if countryW19==2
replace vsw_likeld=0.06*likeLDW17 if countryW19==3

gen vsw_likebrexit=0.02*likeBrexitPartyW17 if countryW19==1
replace vsw_likebrexit=0.005*likeBrexitPartyW17 if countryW19==2
replace vsw_likebrexit=0.054*likeBrexitPartyW17 if countryW19==3

gen vsw_likegrn=0.03*likeGrnW17 if countryW19==1
replace vsw_likegrn=0.01*likeGrnW17 if countryW19==2
replace vsw_likegrn=0.01*likeGrnW17 if countryW19==3

gen vsw_likesnp=0.45*likeSNPW17 if countryW19==2

gen vsw_likepc=0.099*likePCW17 if countryW19==3

* Then, calculate the mean by summing these scores up 

drop mean_ldl_all 

egen mean_ldl_all=rowtotal(vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc)

* drop vsw vars

drop vsw_likecon vsw_likelab vsw_likeld vsw_likebrexit vsw_likegrn vsw_likesnp vsw_likepc

* compare this with the likescore_n variable to make sure I am now picking up those Rs with non-complete data across party like scores. 

tab likescore_n_W17 if mean_ldl_all!=.

** Computing AP scores

* First, compute a party-specific AP component for each individual, with different vote share weights by country

gen ap_con=0.472*((likeConW17 - mean_ldl_all)^2) if countryW19==1
replace ap_con=0.251*((likeConW17 - mean_ldl_all)^2) if countryW19==2
replace ap_con=0.361*((likeConW17 - mean_ldl_all)^2) if countryW19==3

gen ap_lab=0.34*((likeLabW17 - mean_ldl_all)^2) if countryW19==1
replace ap_lab=0.186*((likeLabW17 - mean_ldl_all)^2) if countryW19==2
replace ap_lab=0.409*((likeLabW17 - mean_ldl_all)^2) if countryW19==3

gen ap_ld=0.124*((likeLDW17 - mean_ldl_all)^2) if countryW19==1
replace ap_ld=0.095*((likeLDW17 - mean_ldl_all)^2) if countryW19==2
replace ap_ld=0.06*((likeLDW17 - mean_ldl_all)^2) if countryW19==3

gen ap_brexit=0.02*((likeBrexitPartyW17 - mean_ldl_all)^2) if countryW19==1
replace ap_brexit=0.005*((likeBrexitPartyW17 - mean_ldl_all)^2) if countryW19==2
replace ap_brexit=0.054*((likeBrexitPartyW17 - mean_ldl_all)^2) if countryW19==3

gen ap_grn=0.03*((likeGrnW17 - mean_ldl_all)^2) if countryW19==1
replace ap_grn=0.01*((likeGrnW17 - mean_ldl_all)^2) if countryW19==2
replace ap_grn=0.01*((likeGrnW17 - mean_ldl_all)^2) if countryW19==3

gen ap_snp=0.45*((likeSNPW17 - mean_ldl_all)^2) if countryW19==2

gen ap_pc=0.099*((likePCW17 - mean_ldl_all)^2) if countryW19==3

* The penultimate step is to add these all together. if I use rowtotal, then missing values will be treated as 0 values. 

drop affpol_all

egen affpol_all=rowtotal(ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc)

* drop the ap vars

drop ap_con ap_lab ap_ld ap_brexit ap_grn ap_snp ap_pc

* And then sqrt() this summed variable. 

gen affpol_nlw_W17=sqrt(affpol_all)

summ affpol_nlw_W17
summ affpol_nlw_W19

* Then - again - tab this with the likescore_n variable make sure the affpol score now includes people with incomplete scores across the like score set. 

tab likescore_n_W17 if affpol_nlw_W17!=.


* And now, the final step is to only retain those individuals who provide like scores for at least two parties at both the pre-election and post-election waves 

gen affpol_nlw_ich_W19=affpol_proximate_W19-affpol_nlw_W17

replace affpol_nlw_ich_W19=. if bw_likescore_n==0|bw_likescore_n==1
replace affpol_nlw_W17=. if bw_likescore_n==0|bw_likescore_n==1

* Then - compare this to the original variable 

summ affpol_nlw_ich_W19 // 21,649 obs - 1304 more than the less proximate variable
summ affpol_nlw_ch_W19

disp 21649 - 20345

tab bw_likescore_n if affpol_nlw_ich_W19!=.


** In-party like 

egen IP_like_MLP_W17=rowmax(likeConW17 likeLabW17 likeLDW17 likeBrexitPartyW17 likeGrnW17 likeSNPW17 likePCW17)

summ IP_like_MLP_W17

gen IP_like_ich_W19=IP_like_proximate_W19-IP_like_MLP_W17

replace IP_like_MLP_W17=. if bw_likescore_n==0|bw_likescore_n==1
replace IP_like_ich_W19=. if bw_likescore_n==0|bw_likescore_n==1

summ IP_like_ich_W19 // 21,649 obs - 1304 more than the less proximate variable (same as affpol)
summ IP_like_ch_W19

** Out-party dislike - single party. 

gen OP_dislike_CON_W17 = .

local winners 1 2 3 4 5 9 7
local likevars likeConW17 likeLabW17 likeLDW17 likeSNPW17 likePCW17 likeBrexitPartyW17 likeGrnW17

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W17 = `var' if winconW19 == 0 & Winner19 == `win'
}

*If they won:

forvalues i = 1/7 {
    local win : word `i' of `winners'
    local var : word `i' of `likevars'
    replace OP_dislike_CON_W17 = `var' if winconW19 == 1 & Second19 == `win'
}

summ OP_dislike_CON_W17

gen OP_dislike_CON_R_W17=(0-OP_dislike_CON_W17)+10
summ OP_dislike_CON_R_W17

gen OP_dislike_S_ich_W19=OP_dislike_S_PE_W19-OP_dislike_CON_R_W17

summ OP_dislike_S_ich_W19 // 19529 obs - 1147 more than less proximate wave var 
summ OP_dislike_S_ch_W19

disp 19529-18382

** Out-party dislike - multi-party 

egen total_like=rowtotal(likeConW17 likeLabW17 likeLDW17 likeSNPW17 likePCW17 likeBrexitPartyW17 likeGrnW17)

egen total_parties=rownonmiss(likeConW17 likeLabW17 likeLDW17 likeSNPW17 likePCW17 likeBrexitPartyW17 likeGrnW17)

bysort id: gen OP_dislike_multi_W17=(total_like-IP_like_MLP_W17)/(total_parties-1)

summ OP_dislike_multi_W17

drop total_like total_parties

gen OP_multi_W17=(0-OP_dislike_multi_W17)+10

summ OP_multi_W17

gen OP_dislike_M_ich_W19=OP_multi_proximate_W19-OP_multi_W17

replace OP_multi_W17=. if bw_likescore_n==0|bw_likescore_n==1
replace OP_dislike_M_ich_W19=. if bw_likescore_n==0|bw_likescore_n==1

summ OP_dislike_M_ich_W19 // 21,649 obs (same as affpol)
summ OP_dislike_M_ch_W19

**# 3g. Merge with campaign spending data 

* first, save the data 

save bes_2019_temp_v3.dta, replace

use bes_2019_temp_v3.dta, clear

* Coding Constituency var for matching

decode pconW19, gen(Constituency)

drop _merge 

* Complete merge

merge m:1 Constituency using campaign_spending_2019.dta

* Complete match from the master data (100%)

keep if _merge==3

** Adjusting spending scores by electorate size

gen all_party_spend_W19=A_con_spend_W19/Electorate19

summ A_con_spend_W19
summ all_party_spend_W19

replace Conservative_spend_W19=Conservative_spend_W19/Electorate19
replace Labour_spend_W19=Labour_spend_W19/Electorate19
replace LD_spend_W19=LD_spend_W19/Electorate19
replace SNP_spend_W19=SNP_spend_W19/Electorate19
replace PC_spend_W19=PC_spend_W19/Electorate19
replace UKIP_spend_W19=UKIP_spend_W19/Electorate19
replace Green_spend_W19=Green_spend_W19/Electorate19

summ Conservative_spend_W19, detail

save bes_2019_temp_v4.dta, replace

** Calculating in-party spending

use bes_2019_temp_v4.dta, clear

* first, identify the identity of the in-party - R's most liked party

* first thing I need to do is identify those individuals who have multiple parties with the same max like score


gen match_count_W16 = ///
    cond(!missing(MLP_W16) & MLP_W16 == likeConW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likeLabW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likeLDW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likeGrnW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likeBrexitPartyW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likeSNPW16, 1, 0) + ///
    cond(!missing(MLP_W16) & MLP_W16 == likePCW16, 1, 0)
	
tab match_count_W16

* Now i can code an in-party identifier, using the same numeric structure as for other variables. Con=1; Lab=2; LD=3; SNP=4; PC = 5; UKIP = 6; Green = 7; Other = 8; Brexit= 9.

* First, generate the variable
gen IP_id = .

* Then define the party variables and corresponding ID values

local parties likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16
local ids 1 2 3 4 5 9 7

* Coding a loop. 

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_id = `id' if MLP_W16 == `var' & match_count_W16 == 1
}


* for those individuals with multiple parties with the same max like score, I use PID to differentiate. 

tab MLP_W16

tab partyIdW16
tab partyIdSqueezeW16 if partyIdW16==10

replace IP_id=partyIdW16 if match_count_W16>1&partyIdW16<8
replace IP_id=9 if match_count_W16>1&(partyIdW16==12|partyIdW16==6)
replace IP_id=partyIdSqueezeW16 if match_count_W16>1&partyIdW16==10&partyIdSqueezeW16<8
replace IP_id=9 if match_count_W16>1&partyIdW16==10&(partyIdSqueezeW16==12|partyIdSqueezeW16==6)

* This is not a perfect solution - there are still some individuals without a in-party, becuase they have no PID. 

tab match_count_W16 if match_count_W16>1&IP_id==.

tab IP_id

** Coding the IP spend var

* generating the variable 

gen IP_spend_W19=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W19 Labour_spend_W19 LD_spend_W19 SNP_spend_W19 PC_spend_W19 UKIP_spend_W19 Green_spend_W19
local ids 1 2 3 4 5 9 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_W19 = `var' if IP_id == `id'
}

summ IP_spend_W19

tab BrexitCon_pact

* replacing spending score as Conservative score for those individuals support the Brexit Party but lived in Constituencies in which the party did not stand:

replace IP_spend_W19=Conservative_spend_W19 if IP_id==9&BrexitCon_pact==1

summ IP_spend_W19

** Coding out-party spend 

bysort id: gen OP_spend_W19=all_party_spend_W19-IP_spend_W19

summ OP_spend_W19

*** PID Spending measures 

* First, code a new in-party ID var that is the in-party for those with a PID; and the MLP for those without a PID. 

tab pid_wsqueeze_w16

gen IP_PID_id=pid_wsqueeze_w16 if pid_wsqueeze_w16!=12|pid_wsqueeze_w16!=9
replace IP_PID_id=9 if pid_wsqueeze_w16==12

tab IP_PID_id

* Then define the party variables and corresponding ID values
local parties likeConW16 likeLabW16 likeLDW16 likeSNPW16 likePCW16 likeBrexitPartyW16 likeGrnW16
local ids 1 2 3 4 5 9 7

* Coding a loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    
    replace IP_PID_id = `id' if MLP_W16 == `var' & (pid_wsqueeze_w16==10|pid_wsqueeze_w16==9999)
}

tab IP_PID_id

** Coding IP spend. First, generate the variable 

gen IP_spend_PID_W19=.

* Then define the party variables and corresponding ID values

local parties Conservative_spend_W19 Labour_spend_W19 LD_spend_W19 SNP_spend_W19 PC_spend_W19 UKIP_spend_W19 Green_spend_W19
local ids 1 2 3 4 5 9 7

* code the loop

forvalues i = 1/7 {
    local var : word `i' of `parties'
    local id  : word `i' of `ids'
    replace IP_spend_PID_W19 = `var' if IP_PID_id == `id'
}

* replacing spending score as Conservative score for those individuals support the Brexit Party but lived in Constituencies in which the party did not stand:

tab BrexitCon_pact

replace IP_spend_PID_W19=Conservative_spend_W19 if IP_id==9&BrexitCon_pact==1

summ IP_spend_PID_W19
summ IP_spend_W19

** Coding out-party spend 

bysort id: gen OP_spend_PID_W19=all_party_spend_W19-IP_spend_PID_W19

summ OP_spend_W19
summ OP_spend_PID_W19


**# 3h. Coding party contact data 

tab IP_id

tab partyContact1W19

tab partyContactConW19
tab partyContactLabW19
tab partyContactLDW19
tab partyContactSNPW19
tab partyContactPCW19
tab partyContactBrexitW19
tab partyContactGrnW19

recode partyContactConW19 partyContactLabW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactBrexitW19 partyContactGrnW19 (9999=.)

* Coding in-party contact 

gen IP_contact_W19=0 if partyContact1W19==0
replace IP_contact_W19=1 if partyContactConW19==1&IP_id==1
replace IP_contact_W19=1 if partyContactLabW19==1&IP_id==2
replace IP_contact_W19=1 if partyContactLDW19==1&IP_id==3
replace IP_contact_W19=1 if partyContactSNPW19==1&IP_id==4
replace IP_contact_W19=1 if partyContactPCW19==1&IP_id==5
replace IP_contact_W19=1 if (partyContactBrexitW19==1&IP_id==9)|(partyContactConW19==1&IP_id==9&BrexitCon_pact==1)
replace IP_contact_W19=1 if IP_id==7&partyContactGrnW19==1

tab IP_contact_W19

* Coding out-party contact 
* Going to use the self-reported contact variables for specific out-parties to code this, rather than inferring from the in-party variable, because I think it is important that the respondent specifically reports out-party contact

egen OP_contact_W19=rowtotal(partyContactLabW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactBrexitW19 partyContactGrnW19) if IP_id==1
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactBrexitW19 partyContactGrnW19) if IP_id==2
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLabW19 partyContactSNPW19 partyContactPCW19 partyContactBrexitW19 partyContactGrnW19) if IP_id==3
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLabW19 partyContactLDW19 partyContactPCW19 partyContactBrexitW19 partyContactGrnW19) if IP_id==4
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLabW19 partyContactLDW19 partyContactBrexitW19 partyContactGrnW19) if IP_id==5
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLabW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactGrnW19) if IP_id==9&BrexitCon_pact==0
ereplace OP_contact_W19=rowtotal(partyContactLabW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactGrnW19) if IP_id==9&BrexitCon_pact==1
ereplace OP_contact_W19=rowtotal(partyContactConW19 partyContactLabW19 partyContactLDW19 partyContactSNPW19 partyContactPCW19 partyContactBrexitW19) if IP_id==7

tab OP_contact_W19
tab IP_contact_W19
tab IP_id

* Tactical voting indicator 

tab generalElectionVoteW19

tab pid_wsqueeze_w16

gen tactical_voteW19=1 if (generalElectionVoteW19!=pid_wsqueeze_w16) & ((generalElectionVoteW19<14&generalElectionVoteW19!=10)&(pid_wsqueeze_w16<13&pid_wsqueeze_w16!=10))
replace tactical_voteW19=0 if (generalElectionVoteW19==pid_wsqueeze_w16) & ((generalElectionVoteW19<14&generalElectionVoteW19!=10)&(pid_wsqueeze_w16<13&pid_wsqueeze_w16!=10))

tab tactical_voteW19

**# 3i. Saving the data and preparing for merge

keep affpol* IP_* OP_* countryW19 genderW19 ethnicW19 ageW19 higheducW19 pid_pre_W19 pid_pre_wsqueeze_W19 pid_strength_W19 wingovW19 winconW19 CI_19 panoW19 eff_parties19 raw_margin_W19 pconW19 id AP_R_* AP_distance* farright_ep_W19 Constituency A_con_spend_W19 all_party_spend_W19 R_con_spend_2019  IP_contact_W19 OP_contact_W19 tactical_voteW19

save bes_2019_temp_v5.dta, replace


**# 4. Merging the waves together 

*** 2015 

use bes_2015_temp_v5.dta, clear

** First, the AP variables need to be renamed to pre- and post-election variables 

* Affective polarization

rename affpol_nlw_W3 affpol_nlw_LAG_W6
rename affpol_nlw_W4 affpol_nlw_MPLAG_W6

* In-party like:

rename IP_like_MLP_W6 IP_like_PE_W6
rename IP_like_MLP_W3 IP_like_LAG_W6
rename IP_like_MLP_W4 IP_like_MPLAG_W6
rename IP_like_PID_W3 IP_like_PID_LAG_W6

* Out-party dislike - single 

rename OP_dislike_CON_R_W3 OP_dislike_S_LAG_W6
rename OP_dislike_CON_R_W4 OP_dislike_S_MPLAG_W6

* Out-party dislike - multi 

rename OP_multi_W6 OP_dislike_M_PE_W6
rename OP_multi_W3 OP_dislike_M_LAG_W6
rename OP_multi_W4 OP_dislike_M_MPLAG_W6
rename OP_multi_PID_W3 OP_dislike_M_PID_LAG_W6

* Alternative AP measures 

rename AP_R_W6 AP_R_PE_W6
rename AP_R_W3 AP_R_LAG_W6

rename AP_distance_W6 AP_distance_PE_W6
rename AP_distance_W3 AP_distance_LAG_W6

* Competitiveness and other bits 

rename CI_15 CI_W6
rename eff_parties15 eff_parties_W6

** Retaining only the study vars 

keep affpol_nlw_PE_W6 affpol_nlw_LAG_W6 affpol_nlw_MPLAG_W6 affpol_nlw_ch_W6 affpol_nlw_ich_W6 IP_like_PE_W6 IP_like_LAG_W6 IP_like_ch_W6 IP_like_ich_W6 IP_like_MPLAG_W6 OP_dislike_S_PE_W6 OP_dislike_S_LAG_W6 OP_dislike_S_ch_W6 OP_dislike_S_ich_W6 OP_dislike_S_MPLAG_W6 OP_dislike_M_PE_W6 OP_dislike_M_LAG_W6 OP_dislike_M_ch_W6 OP_dislike_M_ich_W6 OP_disike_M_PID_ch_W6 OP_dislike_M_MPLAG_W6 AP_R_PE_W6 AP_R_LAG_W6 AP_distance_PE_W6 AP_distance_LAG_W6 AP_R_ch_W6 AP_distance_ch_W6 countryW6 genderW6 ethnicW6 ageW6 higheducW6 pid_pre_W6 pid_pre_wsqueeze_W6 pid_strength_W6 wingovW6 winconW6 CI_W6 panoW6 Constituency pconW6 id farright_ep_W6 A_con_spend_W6 all_party_spend_W6 IP_spend_W6 OP_spend_W6 IP_spend_W6 OP_spend_W6 raw_margin_W6 IP_contact_W6 OP_contact_W6 eff_parties_W6 IP_like_PID_ch_W6 IP_like_PID_LAG_W6 OP_disike_M_PID_ch_W6 OP_dislike_M_PID_LAG_W6 IP_spend_PID_W6 OP_spend_PID_W6 tactical_voteW6

* Coding an election indicator for election FE

gen electionW6=1

duplicates report id
drop if id==.

save bes_2015_4merge.dta, replace

*** 2017 

use bes_2017_temp_v6.dta, clear

** First, the AP variables need to be renamed to pre- and post-election variables 

* Affective polarization

rename affpol_nlw_W10 affpol_nlw_LAG_W13
rename affpol_nlw_W11 affpol_nlw_MPLAG_W13

* In-party like:

rename IP_like_MLP_W10 IP_like_LAG_W13
rename IP_like_MLP_W11 IP_like_MPLAG_W13
rename IP_like_PID_W10 IP_like_PID_LAG_W13

* Out-party dislike - single 

rename OP_dislike_CON_R_W10 OP_dislike_S_LAG_W13
rename OP_dislike_CON_R_W11 OP_dislike_S_MPLAG_W13

* Out-party dislike - multi 

rename OP_multi_W13 OP_dislike_M_PE_W13
rename OP_multi_W10 OP_dislike_M_LAG_W13
rename OP_multi_W11 OP_dislike_M_MPLAG_W13
rename OP_multi_PID_W10 OP_dislike_M_PID_LAG_W13

* Alternative AP measures 

rename AP_R_W13 AP_R_PE_W13
rename AP_R_W10 AP_R_LAG_W13

rename AP_distance_W13 AP_distance_PE_W13
rename AP_distance_W10 AP_distance_LAG_W13

* Competitiveness and effective parties

rename CI_17 CI_W13
rename eff_parties17 eff_parties_W13

** Retaining only the study vars 

keep affpol_nlw_PE_W13 affpol_nlw_LAG_W13 affpol_nlw_MPLAG_W13 affpol_nlw_ch_W13 affpol_nlw_ich_W13 IP_like_PE_W13 IP_like_LAG_W13 IP_like_MPLAG_W13 IP_like_ch_W13 IP_like_ich_W13 OP_dislike_S_PE_W13 OP_dislike_S_LAG_W13 OP_dislike_S_ch_W13 OP_dislike_S_ich_W13 OP_dislike_S_MPLAG_W13 OP_dislike_M_PE_W13 OP_dislike_M_LAG_W13 OP_dislike_M_ch_W13 OP_dislike_M_ich_W13 OP_dislike_M_MPLAG_W13 AP_R_PE_W13 AP_R_LAG_W13 AP_distance_PE_W13 AP_distance_LAG_W13 AP_R_ch_W13 AP_distance_ch_W13 countryW13 genderW13 ethnicW13 ageW13 higheducW13 pid_pre_W13 pid_pre_wsqueeze_W13 pid_strength_W13 wingovW13 winconW13 CI_W13 panoW13 Constituency pconW13 id  A_con_spend_W13 all_party_spend_W13 IP_spend_W13 OP_spend_W13 farright_ep_W13 raw_margin_W13 IP_contact_W13 OP_contact_W13 eff_parties_W13 IP_like_PID_ch_W13 IP_like_PID_LAG_W13 OP_disike_M_PID_ch_W13 OP_dislike_M_PID_LAG_W13 IP_spend_PID_W13 OP_spend_PID_W13 tactical_voteW13


* Coding an election indicator for election FE

gen electionW13=2

duplicates report id
drop if id==.

save bes_2017_4merge.dta, replace

*** 2019

use bes_2019_temp_v5.dta, clear

** First, the AP variables need to be renamed to pre- and post-election variables 

* Affective polarization

rename affpol_nlw_W19 affpol_nlw_PE_W19
rename affpol_nlw_W16 affpol_nlw_LAG_W19
rename affpol_nlw_W17 affpol_nlw_MPLAG_W19

* In-party like:

rename IP_like_MLP_W16 IP_like_LAG_W19
rename IP_like_MLP_W17 IP_like_MPLAG_W19
rename IP_like_PID_W16 IP_like_PID_LAG_W19

* Out-party dislike - single 

rename OP_dislike_CON_R_W16 OP_dislike_S_LAG_W19
rename OP_dislike_CON_R_W17 OP_dislike_S_MPLAG_W19

* Out-party dislike - multi 

rename OP_multi_W16 OP_dislike_M_LAG_W19
rename OP_multi_W17 OP_dislike_M_MPLAG_W19
rename OP_multi_PID_W16 OP_dislike_M_PID_LAG_W19

* Alternative AP measures 

rename AP_R_W19 AP_R_PE_W19
rename AP_R_W16 AP_R_LAG_W19

rename AP_distance_W19 AP_distance_PE_W19
rename AP_distance_W16 AP_distance_LAG_W19

* Competitiveness

rename CI_19 CI_W19
rename eff_parties19 eff_parties_W19

** Retaining only the study vars 

keep affpol_nlw_PE_W19 affpol_nlw_LAG_W19 affpol_nlw_MPLAG_W19 affpol_nlw_ch_W19 affpol_nlw_ich_W19 IP_like_PE_W19 IP_like_LAG_W19 IP_like_ch_W19 IP_like_ich_W19 IP_like_MPLAG_W19 OP_dislike_S_PE_W19 OP_dislike_S_LAG_W19 OP_dislike_S_ch_W19 OP_dislike_S_ich_W19 OP_dislike_S_MPLAG_W19 OP_dislike_M_PE_W19 OP_dislike_M_LAG_W19 OP_dislike_M_ch_W19 OP_dislike_M_ich_W19 OP_dislike_M_MPLAG_W19 AP_R_PE_W19 AP_R_LAG_W19 AP_distance_PE_W19 AP_distance_LAG_W19 AP_R_ch_W19 AP_distance_ch_W19 countryW19 genderW19 ethnicW19 ageW19 higheducW19 pid_pre_W19 pid_pre_wsqueeze_W19 pid_strength_W19 wingovW19 winconW19 CI_W19 panoW19 Constituency pconW19 id A_con_spend_W19 all_party_spend_W19 IP_spend_W19 OP_spend_W19 farright_ep_W19 raw_margin_W19 IP_contact_W19 OP_contact_W19 eff_parties_W19 IP_like_PID_ch_W19 IP_like_PID_LAG_W19 OP_disike_M_PID_ch_W19 OP_dislike_M_PID_LAG_W19 IP_spend_PID_W19 OP_spend_PID_W19 tactical_voteW19


* Coding an election indicator for election FE

gen electionW19=3

duplicates report id
drop if id==.

save bes_2019_4merge.dta, replace

*** Merging the three waves together

use bes_2019_4merge.dta, replace

merge 1:1 id using bes_2017_4merge.dta

drop _merge

merge 1:1 id using bes_2015_4merge.dta

save bes_2015_2019_wide.dta, replace

use bes_2015_2019_wide.dta, clear

reshape long affpol_nlw_PE_W affpol_nlw_LAG_W affpol_nlw_MPLAG_W affpol_nlw_ch_W affpol_nlw_ich_W IP_like_PE_W IP_like_LAG_W IP_like_ch_W IP_like_ich_W OP_dislike_S_PE_W OP_dislike_S_LAG_W OP_dislike_S_ch_W OP_dislike_S_ich_W OP_dislike_M_PE_W OP_dislike_M_LAG_W OP_dislike_M_ch_W  OP_dislike_M_ich_W AP_R_PE_W AP_R_LAG_W AP_distance_PE_W AP_distance_LAG_W AP_R_ch_W AP_distance_ch_W countryW genderW ethnicW ageW higheducW pid_pre_W pid_pre_wsqueeze_W pid_strength_W wingovW winconW CI_W panoW pconW all_party_spend_W A_con_spend_W IP_spend_W OP_spend_W farright_ep_W electionW  IP_like_MPLAG_W OP_dislike_S_MPLAG_W OP_dislike_M_MPLAG_W raw_margin_W IP_contact_W OP_contact_W eff_parties_W IP_like_PID_ch_W IP_like_PID_LAG_W OP_disike_M_PID_ch_W OP_dislike_M_PID_LAG_W IP_spend_PID_W OP_spend_PID_W tactical_voteW, i(id) j(wave)

** Labelling factor vars 

* Election 

tab electionW

label define election_label 1 "2015" 2 "2017" 3 "2019"
label values electionW election_label
tab electionW

* Wave 

tab wave

label define wave_label 6 "2015" 13 "2017" 19 "2019"
label values wave wave_label
tab wave

* country 

tab countryW

* pid vars 

tab pid_pre_W
tab pid_strength_W

save bes_2015_2019_long_v1.dta, replace

tab OP_contact_W
tab IP_contact_W

pwcorr OP_contact_W IP_contact_W

**# ----- DESCRIPTIVES AND GRAPHS -----

**# Correlations 

tab pid_pre_W

tab eff_parties_W

* Between the two campaign effort measures and competitiveness 

pwcorr IP_spend_W OP_spend_W CI_W affpol_nlw_PE_W IP_like_PE_W OP_dislike_S_PE_W OP_dislike_M_PE_W

* Between the two self-reported contact measures 

pwcorr OP_contact_W IP_contact_W


**# 1. Individual-level descriptives

use bes_2015_2019_long_v1.dta, clear 

* CI index mean and SD 

summ CI_W

* Mean = 0.607; SD = 0.178. 

* correlation between campaign spending and competitiveness

* In-party spending and competitiveness: r = 0.1325. 

pwcorr CI_W IP_spend_W

* Out-party spending and competitiveness: r = - 0.0493 

pwcorr CI_W OP_spend_W


*** Mean affective polarization, IP like and OP dislike scores, pre- and post-election

** Affective polarization

use bes_2015_2019_wide.dta, clear

* 2015

summ affpol_nlw_LAG_W6 // 2.6

summ affpol_nlw_ch_W6 // + 0.27

disp 0.27/2.6*100

* 2017

summ affpol_nlw_LAG_W13 // 2.47

summ affpol_nlw_ch_W13 // + 0.34

disp 0.34/2.47*100

* 2019

summ affpol_nlw_LAG_W19 // 2.44

summ affpol_nlw_ch_W19 // + 0.36

**# 2. Graphs 

**# --- Bar plot for effective parties

use BES-2019-General-Election-results-file-v1.1.dta, clear

** First, recode the effective party measure at each election

** 2015 

* Dropping speaker's constituency 

tab Winner15
drop if Winner15==9

* Coding missing as 0 

tab Green15
replace Green15=0 if Green15==.
tab Other15
replace Other15=0 if Other15==.
tab UKIP15
replace UKIP15=0 if UKIP15==.
tab SNP15
replace SNP15=0 if SNP15==.
tab PC15
replace PC15=0 if PC15==.

* Coding vote share vars 

gen ConVs15=Con15/100
gen LabVs15=Lab15/100
gen LDVs15=LD15/100
gen UKIPVs15=UKIP15/100
gen GrnVs15=Green15/100
gen SNPVs15=SNP15/100
gen PCVs15=PC15/100
gen OtherVs15=Other15/100

* Calculating effective number of parties

bysort ConstituencyName: gen eff_part_15 = 1/((ConVs15)^2 + (LabVs15)^2 + (LDVs15)^2 + (UKIPVs15)^2 + (GrnVs15)^2 + (SNPVs15)^2 + (PCVs15)^2 + (OtherVs15)^2)

tab eff_part_15

* Round: 

gen eff_parties2015=.
replace eff_parties2015 = 1 if eff_part_15<1.5
replace eff_parties2015 = 2 if eff_part_15>=1.5 & eff_part_15<2.5 
replace eff_parties2015 = 3 if ((eff_part_15>=2.5) & (eff_part_15<3.5))
replace eff_parties2015 = 4 if eff_part_15>3.5

** 2017 

* Dropping speaker's constituency 

tab Winner17
drop if Winner17==9

* Coding vote share vars 

gen ConVs17=Con17/100
gen LabVs17=Lab17/100
gen LDVs17=LD17/100
gen UKIPVs17=UKIP17/100
gen GrnVs17=Green17/100
gen SNPVs17=SNP17/100
gen PCVs17=PC17/100
gen OtherVs17=Other17/100

* Coding missing as 0 

recode ConVs17 LabVs17 PCVs17 SNPVs17 OtherVs17 GrnVs17 UKIPVs17 LDVs17 (.=0)

* Calculating effective number of parties

bysort ConstituencyName: gen eff_part_17 = 1/((ConVs17)^2 + (LabVs17)^2 + (LDVs17)^2 + (UKIPVs17)^2 + (GrnVs17)^2 + (SNPVs17)^2 + (PCVs17)^2 + (OtherVs17)^2)

tab eff_part_17

* Round:

gen eff_parties2017=.
replace eff_parties2017 = 1 if eff_part_17<1.5
replace eff_parties2017 = 2 if eff_part_17>=1.5 & eff_part_17<2.5 
replace eff_parties2017 = 3 if ((eff_part_17>=2.5) & (eff_part_17<3.5))
replace eff_parties2017 = 4 if eff_part_17>3.5
tab eff_parties2017

** 2019

* Dropping speaker's constituency 

tab Winner19
drop if Winner19==9
 
 * Coding vote share vars

gen ConVs19=Con19/100
gen LabVs19=Lab19/100
gen LDVs19=LD19/100
gen UKIPVs19=UKIP19/100
gen GrnVs19=Green19/100
gen SNPVs19=SNP19/100
gen PCVs19=PC19/100
gen OtherVs19=Other19/100
gen BrexitVs19=Brexit19/100

* Coding missing as 0 

recode ConVs19 LabVs19 PCVs19 SNPVs19 OtherVs19 GrnVs19 UKIPVs19 LDVs19 BrexitVs19 (.=0)

* Calculating effective number of parties 

bysort ConstituencyName: gen eff_part_19 = 1/((ConVs19)^2 + (LabVs19)^2 + (LDVs19)^2 + (UKIPVs19)^2 + (GrnVs19)^2 + (SNPVs19)^2 + (PCVs19)^2 + (OtherVs19)^2)

tab eff_part_19

* Round: 

gen eff_parties2019=.
replace eff_parties2019 = 1 if eff_part_19<1.5
replace eff_parties2019 = 2 if eff_part_19>=1.5 & eff_part_19<2.5 
replace eff_parties2019 = 3 if ((eff_part_19>=2.5) & (eff_part_19<3.5))
replace eff_parties2019 = 4 if ((eff_part_19>3.5) & (eff_part_19<4.5))
replace eff_parties2019 = 5 if eff_part_19>4.5

* keeping only the necessary variables for the graph

keep eff_parties* ConstituencyName

* Create the plot 
* individual plots for each year

graph bar, over(eff_parties2015) ///
 bar(1, color(gs4)) ///
 ytitle("Percentage", size(*1.4)) ///
b1title(Effective no. of candidates, size(*1.2)) ///
title("2015") ///
    name(g2015, replace)
	
	graph bar, over(eff_parties2017) ///
 bar(1, color(gs4)) ///
 ytitle("") ///
b1title(Effective no. of candidates, size(*1.2)) ///
title("2017") ///
    name(g2017, replace)
	
		graph bar, over(eff_parties2019) ///
 bar(1, color(gs4)) ///
 ytitle("") ///
b1title(Effective no. of candidates, size(*1.2)) ///
title("2019") ///
    name(g2019, replace)
	
* and then combine the plots 
	graph combine g2015 g2017 g2019, row(1) ycommon

**# --- Strip plot for competitiveness 

* Loading the CI data for 2015 

use CI_2015_v2.dta, replace

* Retain only necessary vars for the plot

keep ConstituencyName CI_15
rename CI_15 CI_2015

* Merge with 2017

merge 1:1 ConstituencyName using CI_2017_v2.dta

rename CI_17 CI_2017

* Retain only necessary vars for the plot

keep ConstituencyName CI_*

* Merge with 2019

merge 1:1 ConstituencyName using CI_2019_v2.dta

rename CI_19 CI_2019

* Retain only necessary vars for the plot

keep ConstituencyName CI_*

* Reshape long

reshape long CI_ , i(ConstituencyName) j(year)

* Save for exporting into R

save CI_plot.dta, replace

**# --- Plots for campaign spending 

*** Strip plot 

* Loading the campaign spending data for 2015 

use campaign_spending_2015.dta, clear

* Merge with the 2017 spending data

merge 1:1 Constituency using campaign_spending_2017.dta

drop _merge 

* Merge with the 2019 spending data

merge 1:1 Constituency using campaign_spending_2019.dta

* Retain only necessary vars

  keep A_con_* Constituency
  
 rename A_con_spend_W6 absolute_spend_2015
 rename A_con_spend_W13 absolute_spend_2017
 rename A_con_spend_W19 absolute_spend_2019
  

* Reshape long 

  reshape long absolute_spend_ , i(Constituency) j(year)
  
* Save for exporting into R
  
save spending_plot.dta, replace


*** Party spend over time 
  
  * Loading the campaign spending data for 2015 

use campaign_spending_2015.dta, clear

* Merge with the 2017 spending data

merge 1:1 Constituency using campaign_spending_2017.dta

drop _merge 

* Merge with the 2019 spending data

merge 1:1 Constituency using campaign_spending_2019.dta

* Retain only necessary vars

drop  A_con_* R_con_*

* Reshape long 

  reshape long Conservative_spend_W Labour_spend_W LD_spend_W SNP_spend_W PC_spend_W UKIP_spend_W Green_spend_W, i(Constituency) j(wave)
  
** Coding wave-specific mean vars

bysort wave: egen Conservative_spend_mean=mean(Conservative_spend_W)

bysort wave: egen Labour_spend_mean=mean(Labour_spend_W)

bysort wave: egen LD_spend_mean=mean(LD_spend_W)

bysort wave: egen SNP_spend_mean=mean(SNP_spend_W)

bysort wave: egen PC_spend_mean=mean(PC_spend_W)

bysort wave: egen UKIP_spend_mean=mean(UKIP_spend_W)

bysort wave: egen Green_spend_mean=mean(Green_spend_W)


 *** make the plot 
  
 graph twoway scatter Conservative_spend_mean wave, msym(O) connect(l) color(navy) ///
 || scatter Labour_spend_mean wave, msym(O) connect(l) color(red) ///
 || scatter LD_spend_mean wave, msym(O) connect(l) color(orange) ///
 || scatter SNP_spend_mean wave, msym(O) connect(l) color(yellow) ///
 || scatter PC_spend_mean wave, msym(O) connect(l) color(dkgreen) ///
 || scatter UKIP_spend_mean wave, msym(O) connect(l) color(purple) ///
 || scatter Green_spend_mean wave, msym(O) connect(l) color(lime) ///
ytitle("Mean Constituency Spend (£)") ///
xtitle("Year") ///
legend(title("Party", size(medium)) order(1 "Conservatives" 2 "Labour" 3 "Lib Dems" 4 "SNP" 5 "PC" 6 "UKIP/Brexit" 7 "Greens")) ///
xlabel(6 "2015" 13 "2017" 19 "2019") xscale(r(6 19))




